2 BytesC++ course_2014_c6_ constructors and other tools
-
Upload
kinan-keshkeh -
Category
Software
-
view
151 -
download
0
Transcript of 2 BytesC++ course_2014_c6_ constructors and other tools
![Page 1: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/2.jpg)
Welcome !
![Page 3: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/3.jpg)
Constructors and Other Tools
![Page 4: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/4.jpg)
Constructors !
![Page 5: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/6.jpg)
Constructors
![Page 7: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/8.jpg)
Constructors
![Page 9: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/9.jpg)
Constructors
second method to definition constructor It’s in “ : “
![Page 10: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/11.jpg)
Constructors
![Page 12: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/12.jpg)
Constructors
What is it for ?
![Page 13: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/13.jpg)
Constructors
It’s for the class.. OOOnly inside of it !
![Page 14: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/14.jpg)
Constructors
![Page 15: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/15.jpg)
Constructors
Output :
![Page 16: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/17.jpg)
Constructors
![Page 18: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/18.jpg)
Constructors
![Page 19: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/19.jpg)
Constructors
![Page 20: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/20.jpg)
Constructors
![Page 21: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/21.jpg)
Constructors
Output :
![Page 22: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/22.jpg)
Constructors
• Calling by value and by reference in functions !!
![Page 23: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/23.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/24.jpg)
Constructors
• Calling by value and by reference in functions !!
![Page 25: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/25.jpg)
Constructors
• Constant functions in classes !
![Page 26: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/27.jpg)
Constructors
• Constant functions in classes !
![Page 28: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/30.jpg)
Inline Functions !
![Page 31: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/33.jpg)
Static members !
![Page 34: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/36.jpg)
Static members
![Page 37: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/37.jpg)
Static members
Initialize out of class once !!
![Page 38: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/38.jpg)
Static members
![Page 39: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/39.jpg)
Static members
Output:
![Page 40: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/40.jpg)
NESTED AND LOCAL CLASS DEFINITIONS
![Page 41: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/41.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/42.jpg)
NESTED AND LOCAL CLASS DEFINITIONS
• If it is public , then we use “ :: “ .
![Page 43: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/44.jpg)
Vectors !
![Page 45: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/45.jpg)
Vectors !
![Page 46: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/46.jpg)
Vectors !
![Page 47: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/47.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/48.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/49.jpg)
Vectors
![Page 50: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/50.jpg)
Vectors
Output:
![Page 51: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/51.jpg)
That’s for today
That’s for today !
Bye Bye !
![Page 52: 2 BytesC++ course_2014_c6_ constructors and other tools](https://reader033.fdocuments.in/reader033/viewer/2022042908/58f1a8061a28ab85228b45e5/html5/thumbnails/52.jpg)
2 bytes team
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team