Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of...
-
Upload
ezra-logan -
Category
Documents
-
view
222 -
download
0
Transcript of Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of...
![Page 1: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/1.jpg)
Week 02
Object Oriented Analysis and
Designing
![Page 2: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/2.jpg)
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 .
![Page 3: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/3.jpg)
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;}
![Page 4: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/4.jpg)
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
![Page 5: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/5.jpg)
5
Destructors
• 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
![Page 6: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/6.jpg)
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
![Page 7: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/7.jpg)
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
![Page 8: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/8.jpg)
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
![Page 9: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/9.jpg)
9
#include<iostream.h>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
![Page 10: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/10.jpg)
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() ends6. The destructor if object1 is invoked when the program ends
Output:
![Page 11: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/11.jpg)
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.
![Page 12: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/12.jpg)
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.
![Page 13: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/13.jpg)
13
// derived classes#include <iostream>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
![Page 14: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/14.jpg)
14
Math_gradeEnglish_grade
Set math()Set English()Get math()Get English()
Student Grade
Student A
7680
Set math()Set English()Get math()Get English()
6670
Set math()Set English()Get math()Get English()
Student B
![Page 15: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/15.jpg)
15
#include<iostream.h>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);
};
![Page 16: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/16.jpg)
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;
}
![Page 17: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/17.jpg)
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;
}
![Page 18: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/18.jpg)
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
![Page 19: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/19.jpg)
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 1
Method ImplementationFunction name 1 m
![Page 20: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/20.jpg)
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
![Page 21: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/21.jpg)
21
#include<iostream.h>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<=90)|| (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;
}
![Page 22: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/22.jpg)
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
UpperCaseASCIIChar
![Page 23: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/23.jpg)
23
#include<iostream.h>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<=90)|| (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
![Page 24: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/24.jpg)
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}
![Page 25: Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.](https://reader035.fdocuments.in/reader035/viewer/2022081515/5697bfee1a28abf838cb93f4/html5/thumbnails/25.jpg)
25
// namespaces#include <iostream>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;}
53.1416