2 BytesC++ course_2014_c6_ constructors and other tools

Post on 15-Apr-2017

151 views 0 download

transcript

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 0size() - 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