411 REFERENCE · Wikibooks
C++ Programming/Examples
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
C++ Programmingby examples
Appendix Purpose and Scope
This Appendix to the C++ Programming book will attempt to provide users a complementary practical understanding of the C++ programming language. It will be an aggregation dependent on the lessons and theoretical content already presented on the C++ Programming book, so to consistently list all compilable example programs that are used in the main book, this appendix will be useful to refresh your knowledge in a hands-on approach, guarantee performance and permit easier maintenance of the used code and test derivations of the examples used.
This is an open work, if you find any problems with terms or concepts you can help by contribute to it, your participation is needed and welcomed! You are also welcome to state any preference, shortcomings or vision for the actual book content, structure or other conceptual matters, see this Wikibook's discussion page for the right forum for participating.
80px
This page of the C++ Programming Wikibook is under construction.It may be incomplete or contain errors. See Talk:C++ Programming/Content for discussion.
About the book
Referencing the learning chapters
If you have questions related to C++ that are not addressed on the book, ask at the Q&A or checkSubject:C++ programming language
Preamble
C++ Programming by examples can be used as an alternative approach to learn the language, by putting practice over theory. In any case this will require you to have greater level of expertise. You will need to understand how C++ files are organized, have and know how to operate a compiler and understand some nuances that will not be visible in code. Do understand that you will be able to read and program in C++, to some degree but to get to really understand the language, theory, not only practice, is required. Use the primary book as a reference anytime you have a doubt.
In the book we cover where to get a compiler (see the Compiler Section), this will provide you the tool needed to proceed. If you don't have, want or need a compiler installed on you machine you can use a WEB free compiler available at http://ideone.com (or http://codepad.org but you will have to change the code not to require interactive input).
Chapter 1: C++ a multi-paradigm language
This is the initial chapter of the book, it attempts to introduce the C++ language to the reader, speak of the language history and its evolution. providing an overview in an attempt to convey the reasons why one should come to know the language.
To understand C++, first one must establish what a programming language is, how they relate with each other (Low-level/High-level), the different programming paradigms, the Procedural programming and Object-oriented programming (Objects and Classes, Encapsulation, Inheritance, Multiple inheritance, Polymorphism), Generic programming, etc. As a way to permit to, not only to provide some useful background, but for readers with experience in other languages to understand how particular C++ is as compared to the other languages that are closely related, C (the origin of C++), Java, C#, (C++/CLI) and D.
In this chapter readers are asked to examine the first C++ source code, the common Hello World and provided the resources to understand it in step-by-step way.
Hello World: Writing, Compiling and Running a C++ Program
Chapter 1: Compilable examples
Hello World - Displays a string - Displays a string1 - Displays a string2
Chapter 2: Fundamentals for getting started
Data and Variables
One of the concepts for novice programmers is understanding how to write a program that sorts three integers. This comes in handy if you want to apply this to a program that you will later want to incorporate in a real-life situation. I am using this code to show how this can be applied for the purposes of using this code at work.
One example that comes to mind is if you were counting people such as (1) population density of a region or (2) the number of employees to get the demographics. For example, if you worked at the Census Bureau, and you wanted to measure how much of the population had moved from the Country to the City, you may way to sort the numbers. If you want to know if more women than men applied for a certain job, you will want to sort these the integers. Sometimes these types of Sorting functions can be seen on websites that filter and give the view with the largest integer descending down to the smallest of integers. Behind this User-interface (UI) is a Sorting function that is called when the User clicks a Submit button on a website.
This example takes 3 integers (from the User's Input) and sorts it. The main purpose of this section is to show how simple code can be used in real-life situations.
#include <iostream>
#include <cmath>
using namespace std;
void sortNums(int& input1, int& input2, int& input3);
int main()
{
int x, y, z; // create variables
// Get numbers using cout and cin
cout << "Please enter 3 numbers." << endl;
cin >> x;
cin >> y;
cin >> z;
// Sort numbers: x, y, and z
sortNums(x, y, z); // calls the function titled sortNums
cout << x << ", " << y << ", " << z << endl; // outputs the numbers
return 0;
}
void sortNums(int &myNumA, int &myNumB, int &myNumC) // sort numbers
{
int temp; // create temp variable
if (myNumA > myNumB)
{
temp = myNumA;
myNumA = myNumB;
myNumB = temp;
}
if (myNumB > myNumC)
{
temp = myNumB;
myNumB = myNumC;
myNumC = temp;
}
if (myNumA > myNumB)
{
temp = myNumA;
myNumA = myNumB;
myNumB = temp;
}
}
Chapter 3: Object-Oriented Programming
Chapter 4: Advanced Features
Chapter 5: Beyond the Standard
Average ( same program rewritten using a class )
Complicated Scope (v1 - v2 - v3)
Namespaces
Storing two values in integer variables (v1 - v2)
Using a Class
Displays a string (v1 - v2 - v3)
Using a Class
I/O Streams
6
Appendix C