-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
1/34
Object-Oriented Programming
(OOP)Lecture No. 15
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
2/34
Composition
Conceptual notation:
String()
SetString(char *) : void
GetString() const : const char *
~String()
gpa : floatrollNo : int
name : String
Student(char * = NULL, int = 0,
float = 0.0);
Student(const Student &)
GetName() const : String
GetNamePtr() const : const char *SetName(char *) : void
~Student()
Student
string : char *
String
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
3/34
CompositionMain Function:
int main(){Student aStudent("Fakhir", 899,3.1);
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
4/34
CompositionOutput:
Constructor::String..
Constructor::Student..
Name: Fakhir
Destructor::Student..Destructor::String..
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
5/34
CompositionStudent::Student(char * n,
int roll, float g){
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
6/34
CompositionTo assign meaningful values to theobject, the functionSetStringis called
explicitly in the constructor
This is an overheadSub-object namein the studentclass
can be initialized using the constructor
Member initialization list syntax is used
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
7/34
CompositionAdd an overloaded constructor to theStringclass defined above:
class String{char *ptr;public:
String();
String(char *);String(const String &);void SetName(char *);~String();
};
//String(char * = NULL);
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
8/34
Composition
String::String(char * str){
if(str != NULL){
ptr = new char[strlen(str)+1];strcpy(ptr, str);
}
else ptr = NULL;
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
9/34
CompositionStudent class is modified asfollows:
class Student{
private:float gpa;
int rollNumber;
String name;
public:
Student(char *=NULL, int=0, float=0.0);
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
10/34
CompositionStudent classcontinued:
Student::Student(char * n,int roll,float g): name(n){
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
11/34
CompositionMain Function:
int main(){Student aStudent("Fakhir", 899,3.1);
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
12/34
CompositionOutput:
Overloaded Constructor::String..
Constructor::Student..
Name: Fakhir
Destructor::Student..Destructor::String..
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
13/34
CompositionNow consider the following case:
String
name: char*
String()String(char *)
~String()
Date()
Date(int,int,int)
Date(const Date &)
Student()
Student( char *,
const Date &, int, float)SetName(char *) : void
GetName() : char *
~Student()
name : String
birthDate : Date
Student
day: intMonth: int
year: int
Date
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
14/34
CompositionStudent class is modified asfollows:
class Student{
private:
Date birthDate;
String name;
public:
Student(char *, const Date &, int,float);
~Student();
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
15/34
CompositionStudent class continued:
Student::Student(char * n, const Date & d,
int roll, flaot g): name(n),birthDate(d){cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
16/34
Composition
Main function:
int main(){
Date _date(31, 12, 1982);
Student aStudent("Fakhir",_date,899,3.5);
return 0;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
17/34
CompositionOutput:
Overloaded Constructor::Date..
Copy Constructor::Date..
Overloaded Constructor::String..
Constructor::Student..
Destructor::Student..
Destructor::String..
Destructor::Date..
Destructor::Date..
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
18/34
AggregationComposition vs. AggregationAggregation is a weak relationship
Room(char *, int)
~Room()
FoldChair(int) : bool
Chair()DoSomething() : void
FoldChair() : bool
UnFoldChair() : bool
~Chair()
area : float
chairs[50]:Chair *
Room
Chair
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
19/34
AggregationIn aggregation, a pointer orreference to an object is createdinside a class
The sub-object has a life that is
NOTdependant on the life of itsmaster class
e.g:Chairs can be moved inside or outside atanytime
When Room is destroyed, the chairs may or
may notbe destroyed
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
20/34
Aggregationclass Room{
private:
float area;
Chair * chairs[50];Public:
Room();
void AddChair(Chair *, int chairNo);
Chair * GetChair(int chairNo);bool FoldChair(int chairNo);
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
21/34
Aggregation
Room::Room(){
for(int i = 0; i < 50; i++)
chairs[i] = NULL;}
void Room::AddChair(Chair *chair1, int chairNo){
if(chairNo >= 0 && chairNo < 50)chairs[chairNo] = chair1;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
22/34
AggregationChair * Room::GetChair(int chairNo){
if(chairNo >= 0 && chairNo < 50)
return chairs[chairNo];
else
return NULL;}
bool Room::FoldChair(int chairNo){
if(chairNo >= 0 && chairNo < 50)
return chairs[chairNo]->FoldChair();
else
return false;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
23/34
Aggregationint main(){
Chair ch1;
{
Room r1;r1.AddChair(&ch1, 1);
r1.FoldChair(1);
}ch1.UnFoldChair(1);
return 0;
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
24/34
Friend FunctionsConsider the following class:
class X{
private:
int a, b;
public:
void MemberFunction();
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
25/34
Friend FunctionsGlobal function:
void DoSomething(X obj){
obj.a = 3; //Errorobj.b = 4; //Error
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
26/34
Friend FunctionsIn order to access the member variables of the class,function definition must be made a friend function:
class X{
private:
int a, b;public:
friend void DoSomething(X obj);
}Now the functionDoSomethingcan access datamembers of class X
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
27/34
Friend Functions
Prototypes of friendfunctions appear in the class
definition
But friend functions are NOT
member functions
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
28/34
Friend FunctionsFriend functions can be placed anywhere in theclass without any effect
Access specifiers dont affect friend functions orclasses
class X{
...
private:
friend void DoSomething(X);
public:
friend void DoAnything(X);
...
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
29/34
Friend FunctionsWhile the definition of the friend functionis:
void DoSomething(X obj){obj.a = 3; // No Error
obj.b = 4; // No Error
}
friendkeyword is not given in definition
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
30/34
Friend Functions
If keyword friendis used in thefunction definition, its a syntax error
//Error
friend void DoSomething(X obj){
}
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
31/34
Friend ClassesSimilarly, one class can also be madefriend of another class:
class X{
friend class Y;
};
Member functions of class Y can accessprivate data members of class X
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
32/34
Friend ClassesExample:
class X{
friend class Y;private:
int x_var1, x_var2;...
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
33/34
Friend Classesclass Y{
private:int y_var1, y_var2;
X objX;public:
void setX(){objX.x_var1 = 1;
}};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 15
34/34
Friend Classes
int main(){
Y objY;objY.setX();return 0;
}