+ All Categories
Home > Software > 2 BytesC++ course_2014_c6_ constructors and other tools

2 BytesC++ course_2014_c6_ constructors and other tools

Date post: 15-Apr-2017
Category:
Upload: kinan-keshkeh
View: 151 times
Download: 0 times
Share this document with a friend
52
Kinan keshkeh IT Engineering-Damascus University 3 rd year Summer course- 2014 2 bytes team
Transcript
Page 1: 2 BytesC++ course_2014_c6_ constructors and other tools

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Page 2: 2 BytesC++ course_2014_c6_ constructors and other tools

Welcome !

Page 3: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors and Other Tools

Page 4: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors !

Page 5: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 6: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 7: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

First method to definition constructor (it’s the one which is like other functions definitions)

Page 8: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 9: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

second method to definition constructor It’s in “ : “

Page 10: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

• Call it :

By this by value( … , … )

Or By this clearly !!

Or By this default constructor !!

Page 11: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 12: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

What is it for ?

Page 13: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

It’s for the class.. OOOnly inside of it !

Page 14: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 15: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Output :

Page 16: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 17: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 18: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 19: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 20: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Page 21: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

Output :

Page 22: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

• Calling by value and by reference in functions !!

Page 23: 2 BytesC++ course_2014_c6_ constructors and other tools

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.

Page 24: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

• Calling by value and by reference in functions !!

Page 25: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

• Constant functions in classes !

Page 26: 2 BytesC++ course_2014_c6_ constructors and other tools

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.

Page 27: 2 BytesC++ course_2014_c6_ constructors and other tools

Constructors

• Constant functions in classes !

Page 28: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 29: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 30: 2 BytesC++ course_2014_c6_ constructors and other tools

Inline Functions !

Page 31: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 32: 2 BytesC++ course_2014_c6_ constructors and other tools

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; }

Page 33: 2 BytesC++ course_2014_c6_ constructors and other tools

Static members !

Page 34: 2 BytesC++ course_2014_c6_ constructors and other tools

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)

Page 35: 2 BytesC++ course_2014_c6_ constructors and other tools

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

Page 36: 2 BytesC++ course_2014_c6_ constructors and other tools

Static members

Page 37: 2 BytesC++ course_2014_c6_ constructors and other tools

Static members

Initialize out of class once !!

Page 38: 2 BytesC++ course_2014_c6_ constructors and other tools

Static members

Page 39: 2 BytesC++ course_2014_c6_ constructors and other tools

Static members

Output:

Page 40: 2 BytesC++ course_2014_c6_ constructors and other tools

NESTED AND LOCAL CLASS DEFINITIONS

Page 41: 2 BytesC++ course_2014_c6_ constructors and other tools

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 “ :: “ .

Page 42: 2 BytesC++ course_2014_c6_ constructors and other tools

NESTED AND LOCAL CLASS DEFINITIONS

• If it is public , then we use “ :: “ .

Page 43: 2 BytesC++ course_2014_c6_ constructors and other tools

NESTED AND LOCAL CLASS DEFINITIONS

• Local Class : when a class definition is defined within a function definition , the class is called a local class.

Page 44: 2 BytesC++ course_2014_c6_ constructors and other tools

Vectors !

Page 45: 2 BytesC++ course_2014_c6_ constructors and other tools

Vectors !

Page 46: 2 BytesC++ course_2014_c6_ constructors and other tools

Vectors !

Page 47: 2 BytesC++ course_2014_c6_ constructors and other tools

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.

Page 48: 2 BytesC++ course_2014_c6_ constructors and other tools

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.

Page 49: 2 BytesC++ course_2014_c6_ constructors and other tools

Vectors

Page 50: 2 BytesC++ course_2014_c6_ constructors and other tools

Vectors

Output:

Page 51: 2 BytesC++ course_2014_c6_ constructors and other tools

That’s for today

That’s for today !

Bye Bye !

Page 52: 2 BytesC++ course_2014_c6_ constructors and other tools

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team


Recommended