Kinan keshkeh 
IT Engineering-Damascus University 
3rd year 
Summer course- 2014 
2 bytes team
Welcome !
Constructors and Other Tools
Constructors !
Constructors 
•It is a member function like any function in a class , But !! : 
•It has to be in PUBLIC section !! 
•Its name == class name! 
•Doesn’t return a value ! 
•It is called automatically when an object of the class is declared. or it’s called clearly ! 
•It is used to initialize class objects
Constructors
Constructors 
First method to definition constructor (it’s the one which is like other functions definitions)
Constructors
Constructors 
second method to definition constructor It’s in “ : “
Constructors 
•Call it : 
By this by value( … , … ) 
Or By this clearly !! 
Or By this default constructor !!
Constructors
Constructors 
What is it for ?
Constructors 
It’s for the class.. 
OOOnly inside of it !
Constructors
Constructors 
Output :
Constructors 
•Class type , defines members in another class ! 
•They are members in a class2 so , To initialize them (in the class2 constructor) ,use class1 constructors
Constructors
Constructors
Constructors
Constructors
Constructors 
Output :
Constructors 
•Calling by value and by reference in functions !!
Constructors 
•Calling by value and by reference in functions !! 
•A call-by-reference parameter is more efficient than a call- by-value parameter. 
•A call-by-value parameter is a local variable that is initialized to the value of its argument, so when the function is called there are two copies of the argument. 
•With a call-by-reference parameter, the parameter is just a placeholder that is replaced by the argument, so there is only one copy of the argument.
Constructors 
•Calling by value and by reference in functions !!
Constructors 
•Constant functions in classes !
Constructors 
•Constant functions in classes ! 
• If you have a member function that should not change the value of a call-ing object, you can mark the function with the const modifier 
•the computer will then issue an error message if your function code inadvertently changes the value of the call-ing object.
Constructors 
•Constant functions in classes !
Constructors 
•Constant functions in classes ! 
NOTE: 
If you use const for one parameter 
of a particular type, then you should use it for every other parameter that has that type and that is 
not changed by the function call
Constructors 
•Constant functions in classes ! 
NOTE: 
If you use const for one parameter 
of a particular type, then you should use it for every other parameter that has that type and that is 
not changed by the function call
Inline Functions !
Inline Functions 
•They ‘re used only with tiny functions ! 
•Unlike other functions !! : with inline functions , compiler replaces the whole definition in calling 
•To get inline function , just place the keyword inline before the function declaration and function definition
Inline Functions 
#include <iostream> 
#include<cstdlib> 
using namespace std; 
inline void welcome(int T); 
int main( ) 
{ 
welcome( 5); 
return 0; 
} 
inline void welcome(int t){ 
cout << "Welcome To Our Bank.N" 
<< "The Status Of Your Account Is:N" 
<< t; 
}
Static members !
Static members 
•To get Static members , just place the word ‘ static ‘ before any member ( private or public) . 
•NOTE: static functions don’t take nonstatic members 
•Calling:( ClassName :: staticmemberName) 
•Private Static members: 
•Initialized out of class once only !! 
•Then they still private(only visible inside class members)
Static members 
•To get Static members , just place the word ‘ static ‘ before any member ( private or public) . 
•NOTE: static functions don’t take nonstatic members 
•Calling:( ClassName :: staticmemberName) 
•Private Static members: 
•Initialized out of class once only !! 
•Then they still private(only visible inside class members) 
•Initialize(public&private) once only anyway
Static members
Static members 
Initialize out of class once !!
Static members
Static members 
Output:
NESTED AND LOCAL CLASS DEFINITIONS
NESTED AND LOCAL CLASS DEFINITIONS 
•A nested class can be either public or private . 
•If it is private , then it cannot be used outside of the outer class . 
•If it is public , then we use “ :: “ .
NESTED AND LOCAL CLASS DEFINITIONS 
•If it is public , then we use “ :: “ .
NESTED AND LOCAL CLASS DEFINITIONS 
• Local Class : when a class definition is defined within a function definition , the class is called a local class.
Vectors !
Vectors !
Vectors !
Vectors 
•Like Arrays , partly ! ( elements 0size() - 1 ) 
•It’s a ClassTemplate in Vector library . 
•vector<Base_Type> var_name; 
•vector<int> v; //default constructor producing an empty vector. 
•vector<AClass> record(20); //vector constructor uses the //default constructor for AClass to initialize 20 elements.
Vectors 
•The member function push_back adds an element in the next available position.(back of vector) 
•Changing value : v[i] = 42; 
• size() to determine how many elements are in a vector.
Vectors
Vectors 
Output:
That’s for today 
That’s for today ! 
Bye Bye !
2 bytes team 
Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 
2 bytes team

2 BytesC++ course_2014_c6_ constructors and other tools

  • 1.
    Kinan keshkeh ITEngineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 2.
  • 3.
  • 4.
  • 5.
    Constructors •It isa member function like any function in a class , But !! : •It has to be in PUBLIC section !! •Its name == class name! •Doesn’t return a value ! •It is called automatically when an object of the class is declared. or it’s called clearly ! •It is used to initialize class objects
  • 6.
  • 7.
    Constructors First methodto definition constructor (it’s the one which is like other functions definitions)
  • 8.
  • 9.
    Constructors second methodto definition constructor It’s in “ : “
  • 10.
    Constructors •Call it: By this by value( … , … ) Or By this clearly !! Or By this default constructor !!
  • 11.
  • 12.
  • 13.
    Constructors It’s forthe class.. OOOnly inside of it !
  • 14.
  • 15.
  • 16.
    Constructors •Class type, defines members in another class ! •They are members in a class2 so , To initialize them (in the class2 constructor) ,use class1 constructors
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
    Constructors •Calling byvalue and by reference in functions !!
  • 23.
    Constructors •Calling byvalue and by reference in functions !! •A call-by-reference parameter is more efficient than a call- by-value parameter. •A call-by-value parameter is a local variable that is initialized to the value of its argument, so when the function is called there are two copies of the argument. •With a call-by-reference parameter, the parameter is just a placeholder that is replaced by the argument, so there is only one copy of the argument.
  • 24.
    Constructors •Calling byvalue and by reference in functions !!
  • 25.
  • 26.
    Constructors •Constant functionsin classes ! • If you have a member function that should not change the value of a call-ing object, you can mark the function with the const modifier •the computer will then issue an error message if your function code inadvertently changes the value of the call-ing object.
  • 27.
  • 28.
    Constructors •Constant functionsin classes ! NOTE: If you use const for one parameter of a particular type, then you should use it for every other parameter that has that type and that is not changed by the function call
  • 29.
    Constructors •Constant functionsin classes ! NOTE: If you use const for one parameter of a particular type, then you should use it for every other parameter that has that type and that is not changed by the function call
  • 30.
  • 31.
    Inline Functions •They‘re used only with tiny functions ! •Unlike other functions !! : with inline functions , compiler replaces the whole definition in calling •To get inline function , just place the keyword inline before the function declaration and function definition
  • 32.
    Inline Functions #include<iostream> #include<cstdlib> using namespace std; inline void welcome(int T); int main( ) { welcome( 5); return 0; } inline void welcome(int t){ cout << "Welcome To Our Bank.N" << "The Status Of Your Account Is:N" << t; }
  • 33.
  • 34.
    Static members •Toget Static members , just place the word ‘ static ‘ before any member ( private or public) . •NOTE: static functions don’t take nonstatic members •Calling:( ClassName :: staticmemberName) •Private Static members: •Initialized out of class once only !! •Then they still private(only visible inside class members)
  • 35.
    Static members •Toget Static members , just place the word ‘ static ‘ before any member ( private or public) . •NOTE: static functions don’t take nonstatic members •Calling:( ClassName :: staticmemberName) •Private Static members: •Initialized out of class once only !! •Then they still private(only visible inside class members) •Initialize(public&private) once only anyway
  • 36.
  • 37.
    Static members Initializeout of class once !!
  • 38.
  • 39.
  • 40.
    NESTED AND LOCALCLASS DEFINITIONS
  • 41.
    NESTED AND LOCALCLASS DEFINITIONS •A nested class can be either public or private . •If it is private , then it cannot be used outside of the outer class . •If it is public , then we use “ :: “ .
  • 42.
    NESTED AND LOCALCLASS DEFINITIONS •If it is public , then we use “ :: “ .
  • 43.
    NESTED AND LOCALCLASS DEFINITIONS • Local Class : when a class definition is defined within a function definition , the class is called a local class.
  • 44.
  • 45.
  • 46.
  • 47.
    Vectors •Like Arrays, partly ! ( elements 0size() - 1 ) •It’s a ClassTemplate in Vector library . •vector<Base_Type> var_name; •vector<int> v; //default constructor producing an empty vector. •vector<AClass> record(20); //vector constructor uses the //default constructor for AClass to initialize 20 elements.
  • 48.
    Vectors •The memberfunction push_back adds an element in the next available position.(back of vector) •Changing value : v[i] = 42; • size() to determine how many elements are in a vector.
  • 49.
  • 50.
  • 51.
    That’s for today That’s for today ! Bye Bye !
  • 52.
    2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team