C++

//Content under construction, as I’m migrating the data from my old website 🙂

Key features of object oriented programming (OOP): inheritance – polymorphic and encapsulation (and fundamentally abstraction).

Table of Contents
1. C Programming Basics
Basics
Comments
Variables [Integers – Short – Long — Char — Floating Point – double – long double — Boolean — unsigned data types — Type Conversion]
Casts
Output/Input
Arithmetic Operators
2. Loops & Decisions
Logical operators
Loops [For loop — While loop — Do loop]
Decisions [If — Switch]
Conditional operator — Logical Operator
Other Control Statements
3. Structures
Structures
Enumerations
4. Functions
No arguments
Passing constants as arguments
Passing variables
Overloaded functions
Inline functions
Default arguments
Variables and Storage Classes
Automatic
External
Static
Returning by reference
Const function arguments
5. Objects & Classes
C++ Objects as data types
Constructors
Destructors
Objects as function arguments
The default copy constructor
Returning objects from functions
Card game example
Structures and classes
Static class data – object, classes and memory
const and classes
const objects
6. Arrays & Strings
Arrays Fundamentals
Multidimensional arrays
Arrays as function arguments
Arrays of structures
Arrays as class member data
Arrays of objects
Arrays of cards
C-strings
7. Operator Overloading
8. Inheritance
9. Pointers
10. Virtual Functions
11. Streams & Files
12. Multi-file programs
13. Templates & Exceptions
14. Standard Template Library
15. Object-Oriented Design
Appendix: Necessary Notes
Header files needed for some library functions

1. C Programming Basics

Basics

 

 #include 

using namespace std;

int main()

{

cout << "Hello world!" << endl;

return 0;

}

Comments

#include 

using namespace std;

int main()

{

cout << "Hello world!" << endl; //that's a comment

return 0;

/*

and this is a multi-line

comment*/

}

Variables

Integers – Short – Long

#include 

using namespace std;

int main()

{

int x; //defining variable x as integer; check the size and limits of this variable type based on your OS, variable name is called identifier and it can be letters, numbers or _ and must start with letter or _ and can’t be any of keywords; it’s case sensitive!

int y; //this is called declaration and definition in the same time as it sets space for the variable in the memory; note that declaration need not be in the beginning of the program

short l; //that's a different integer type, with fixed size and range

long p; //that's a different integer type, with fixed size and range

x=20; //assigning 20 to x; it’s called assignment statement

y=x+10; //note that we must terminate with a semicolon; here x and 10 are considered integer constants

cout << "y=" ;

cout<<y<<endl;

return 0;

}

Char

#include 

using namespace std;

int main()

{

char a= 'A'; //that's called "initialization" as give a value with declaring it as a character variable (type of size 1 byte, can store characters as ASCII number)

char b='\t'; // here \t is an escape sequence for tab; a: beep, b:backspace, f:formfeed, n:newline, r:return, \,',"":as they are, xdd:hexadecimal notation

cout << a ;

cout<<b;

a='\f';

cout<<a;

cout<<'\n';

return 0;

}

Floating Point – double – long double

#include 

using namespace std;

int main()

{

float rad=5; //defining variable of type float (other types include long and double)

const float PI=3.14159F; //that's a constant qualifier; makes sure PI doesnt change; note the F to make sure it's float (compare to #define directive as #define PI 3.14)

float area = PI * rad * rad;

cout << "Area = " <<area<<endl;

return 0;

}

Bool

Covered later

unsigned data types

#include 

using namespace std;

int main()

{

int signedVar = 1500000000; //that's our regular signed integer variable (can have a sign + or -)

unsigned int unsignVar=1500000000; //now that's unsigned variable, it has larger range

signedVar=(signedVar*2)/3; //this calculation exceeds the range for signed variable

unsignVar=(unsignVar*2)/3; //this, however is still in the range

cout<<signedVar<<"   "<<unsignVar<<endl; //that's why signedVar is wrong and unsignVar is correct

return 0;

}
-431655765   1000000000

Process returned 0 (0x0)   execution time : 0.080 s

Press any key to continue.

Type Conversion

#include

using namespace std;

int main()

{

int s=7;

float p=12.5F;

double t=s*p; //even though s and p are different variable types, C do it with no problem, other languages don't permit it while it happens here automatically; the compiler changes the s type to float and then p to double (it goes from lowest to highest: char - short - int - long - float - double - double long

cout<<"T = 7*12.5 =  "<<t<<endl;

return 0;

}

T = 7*12.5 =  87.5

Process returned 0 (0x0)   execution time : 0.080 s

Press any key to continue.

Casts

#include

using namespace std;

int main()

{

int x = 1500000000;

x=(x*10)/10; //doing the multiplication step will bring a number beyond range of integer variable type

cout<<"X = "<<x<<endl; //wrong answer of course

x=1500000000;

x=(static_cast(x)*10)/10; //cast is used to manually convert a variable type, it has different kinds, here we use static cast; you use it in this form static_cast(VariableNameToBeConverted), y=static_cast(x)

cout<<"X = "<<x<<endl; //correct answer; note that casting created a temporary variable that stored that big value

return 0;

}

X = 211509811

X = 1500000000

Process returned 0 (0x0)   execution time : 0.080 s

Press any key to continue.

Output/Input

#include

using namespace std;

int main()

{

int ftemp;

cout<<"Welcome to Fahrenheit to Celsius program"<<endlftemp; //store the input to the variable ftemp

int ctemp=(ftemp-32)*5/9; //calculating temperature in C

cout << "Temperature in C = " <<ctemp<<endl; //cout can differentiate between a string constant and a variable, notice the insertion operator,  can be cascaded too for cin

return 0;

}

Welcome to Fahrenheit to Celsius program

Please enter temperature in F

118

Temperature in C = 47

Process returned 0 (0x0)   execution time : 6.801 s

Press any key to continue.

#include

using namespace std;

int main()

{

long pop1=2459517, pop2=18,pop3=9164; //note how we initialized the variables in same line

cout <<"Location "<<"Population"<<endl

<<"New York "<<pop1<

<<"Chicago  "<<pop2<

<<"San Jose "<<pop3<<endl; //the outputs is not well organized; not aligned

return 0;

}

Location Population

New York 2459517

Chicago  18

San Jose 9164

Process returned 0 (0x0)   execution time : 0.080 s

Press any key to continue.

#include

#include //we have to include this header file to use setw manipulator

using namespace std;

int main()

{

long pop1=2459517, pop2=18,pop3=9164;

cout <<setw(8)<<"Location"<<setw(12)<<"Population"<

<<setw(8)<<"New York"<<setw(12)<<pop1<

<<setw(8)<<"Chicagoo"<<setw(12)<<pop2<<endl

<<setw(8)<<"San Jose"<<setw(12)<<pop3<<endl; //the output is lined up to the right

return 0;

}

Location  Population

New York     2459517

Chicagoo                18

San Jose             9164

Process returned 0 (0x0)   execution time : 0.094 s

Press any key to continue.

Arithmetic Operators

#include

using namespace std;

int main()

{

cout<<6%8<<endl<<7%8<<endl<<8%8<<endl<<9%8<<endl<<10%8<<endl; //calculates and outputs the remainder; higher precedence than <<

int x=30;

x+=5; //arithmetic assignment operator, equivalent to x=x+5

cout<<"\nx=x+5=x+5"<<x<<endl; //same we can go for x*=5 x/=7 x%=3

int counter=1;

cout<<"\ncounter="<<++counter<<endl; //increment operator, here prefix, so it increment variable before output

cout<<"counter="<<counter++<<endl; //however here it's postfix, so it increment variable after output

cout<<"counter="<<counter<<endl; //now, it has been incremented, so it outputs 3

return 0;

}

6

7

0

1

2

x=x+5=x+535

counter=2

counter=2

counter=3

Process returned 0 (0x0)   execution time : 0.060 s

Press any key to continue.

 

Logical operators

#include

using namespace std;

int main()

{

cout<<"Enter a number"x;

cout<<"\nNumebr < 10 is counter="<<(x<10)<<endl; //check if the number is < 10 and if true displays 1 and false 0

cout<<"\nNumebr = 10 is counter="<<(x==10)<<endl; //check if the number equals 10 note we use == not = which is assignment

cout< 10 is counter="<10)<10 note that 0 is false and any other value (-13 or 159) is true

return 0;

}

Enter a number

19

Numebr 10 is counter=1

Process returned 0 (0x0) execution time : 2.529 s

Press any key to continue.

Advertisements