Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.

Similar presentations


Presentation on theme: "Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance."— Presentation transcript:

1 Week 02 Object Oriented Analysis and Designing

2 Constructors Constructors are member functions of any class, which are invoked the moment an instance of the class which they belong is created. They are special functions that have the same name as their class. They are also be overloaded. Need for constructors: An object of any class has a copy of its data members, which require initialization before they are used. 2

3 Type of Constructors Default Constructor-: – A constructor that accepts no parameters is known as default constructor. If no constructor is defined then the compiler supplies a default constructor. Class:: Circle() { radius = 0.0;} Parameterized Constructor -: – A constructor that receives arguments/parameters, is called parameterized constructor. Circle::Circle(int r) { radius = r;}

4 class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“\n”; } The first constructor is called The second constructor is called Since radius is a private class data member 4

5 Destructors – Special member function – Same name as class Preceded with tilde ( ~ ) – No arguments – No return value – Cannot be overloaded – Before system reclaims object’s memory Reuse memory for new objects Mainly used to de-allocate dynamic memory locations 5

6 Another class Example This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor 6

7 Time::Time() { hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; } void Time::setTime(int h,int m,int s) { *hour = h; *minute = m; *second = s; } Dynamic locations should be allocated to pointers first 7

8 void Time::printTime() { cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; } Time::~Time() { delete hour; delete minute;delete second; } void main() { Time *t; t= new Time(3,55,54); t->printTime(); t->setHour(7); t->setMinute(17); t->setSecond(43); t->printTime(); delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de-allocate memory locations When executed, the destructor is called 8

9 #include class Test{ public: Test(){cout<<"Constructor Invoked "<<endl;} ~Test(){cout<<"Destructor Invoked "<<endl;} }; Test obj1; int main() {cout<<"main() BEGINS"<<endl; Test obj2; { cout<<"INNER BLOCK BEGINS"<<endl; Test obj3; cout<<"INNER BLOCK ENDS"<<endl; } cout<<"main() ENDs"<<endl; return 0; } Life Cycle of an object 9

10 1.The object, obj1, has global scope, hence its constructor is executed before the execution of main() begins. 2.The object 2 has function scope, hence its constructor is executed after main() beings. 3.The object3 has block scope hence its constructor is executed after the inner block begins. 4.The destructor of object3 is invoked when the inner block ends. 5.The destructor if object 2 is invoked when main() ends 6.The destructor if object1 is invoked when the program ends Output: 10

11 Operator Polymorphism (operator overloading). It provides new meaning for existing operators to enable them to work with the user defined types. Overload operators are generally inherited by derived class. By overloading operators in this way we can give all the classes in a system a common interface, allowing use to perform similar operators on a range of different object. 11

12 Overload + operator Int I,j,k; Float p,q,r; K=i+j; R=p+q; Note: + operator is different in different data type. 12

13 // derived classes #include using namespace std; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; class CTriangle: public CPolygon { public: int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; } 20 10 13

14 Math_grade English_grade Math_grade English_grade Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Student Grade Student A 76 80 76 80 Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() 66 70 66 70 Set math() Set English() Get math() Get English() Set math() Set English() Get math() Get English() Student B 14

15 #include class StudentGrade{ private: int maths_grade; int english_grade; public: StudentGrade(); void setMathsGrade(int grade_in); void setEnglishGrade(int grade_in); int getMathsGrade(); int getEnglishGrade(); StudentGrade operator+(StudentGrade a); }; 15

16 StudentGrade::StudentGrade() { maths_grade=0; english_grade=0; } void StudentGrade::setMathsGrade(int grade_in){ maths_grade=grade_in; } void StudentGrade::setEnglishGrade(int grade_in){ english_grade=grade_in; } int StudentGrade::getMathsGrade(){ return maths_grade; } int StudentGrade::getEnglishGrade(){ return english_grade; } StudentGrade StudentGrade::operator+(StudentGrade a){ int temp_maths=maths_grade + a.maths_grade; int temp_english=english_grade + a.english_grade; StudentGrade temp_student; temp_student.setMathsGrade(temp_maths); temp_student.setEnglishGrade(temp_english); return temp_student; } 16

17 int main(){ StudentGrade studentA,studentB,grade_Total; studentA.setMathsGrade(50); studentA.setEnglishGrade(80); studentB.setEnglishGrade(68); studentB.setMathsGrade(50); grade_Total=studentA+studentB; cout<<grade_Total.getMathsGrade()<<endl; cout<<grade_Total.getEnglishGrade()<<endl ; return 0; } 17

18 Inheritance of overloaded operators Overloaded operators (other than ‘=‘) Are inherited by derived classes, it is Often necessary to redefine their behavior for all classes in hierarchy Maths_grade English_grade Sceince_grade Language grade Technology_grade Student_grade Extended Grade 18

19 Inheritance Polymorphism (Method Polymorphism) Method Polymorphism refers to the use of same method name in different classes in the class hierarchy for different purposes. Moron orphic function 1-1 relationship Polymorphic methods in 1-m relationship Function ImplementationFunction name 1 Method ImplementationFunction name 1 m 19

20 example Method ‘getChar’ convert ASCII code for any alphabetical character to its character. Any other ASCII code is convert to a blank character (space) getChar ASCIIChar ShowChar Character 20

21 #include class ASCIIChar { protected: char character; public: void getChar(int char_number); void showChar(); }; void ASCIIChar::getChar(int char_number) { if((char_number>=65 && char_number =97 && char_number<=122)) { character=char_number; } else{ character=32; } void ASCIIChar::showChar() { cout<<"character is"<<character <<endl;} int main() { ASCIIChar a_char; a_char.getChar(97); a_char.showChar(); return 0; } 21

22 Inheritance Polymorphism……. Assume that we want to extended this class with a derived class which will store only upper case alphanumerical characters Same name but work different. ‘GetChar’ has different form. getChar ASCIIChar ShowChar Character getChar UpperCase ASCIIChar 22

23 #include class ASCIIChar { protected: char character; public: void getChar(int char_number); void showChar(); }; class UpperCaseASCIIChar:public ASCIIChar{ public: void getChar(int char_in); }; void UpperCaseASCIIChar::getChar(int char_in){ ASCIIChar::getChar(char_in); if(character !=32){ character -=32;} } void ASCIIChar::getChar(int char_number) { if((char_number>=65 && char_number =97 && char_number<=122)) { character=char_number; } else{ character=32;} } void ASCIIChar::showChar() { cout<<"character is"<<character <<endl;} int main() { ASCIIChar either_case; UpperCaseASCIIChar upper_case; either_case.getChar(97); upper_case.getChar(97); either_case.showChar(); upper_case.showChar(); return 0;} Inherit 23

24 Namespaces Namespaces allow to group entities like – Classes – Objects – functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The format of namespaces is: namespace identifier { entities } 24

25 // namespaces #include using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { cout << first::var << endl; cout << second::var << endl; return 0; } 5 3.1416 25


Download ppt "Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance."

Similar presentations


Ads by Google