0% found this document useful (0 votes)
2 views

Lecture15

The document discusses Object-Oriented Programming (OOP) concepts, focusing on composition and aggregation, as well as friend functions and classes. It provides examples of class constructors, destructors, and member functions, illustrating how to manage relationships between classes. Additionally, it highlights the differences between composition (strong relationship) and aggregation (weak relationship) in class design.

Uploaded by

shehzadkamran100
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture15

The document discusses Object-Oriented Programming (OOP) concepts, focusing on composition and aggregation, as well as friend functions and classes. It provides examples of class constructors, destructors, and member functions, illustrating how to manage relationships between classes. Additionally, it highlights the differences between composition (strong relationship) and aggregation (weak relationship) in class design.

Uploaded by

shehzadkamran100
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Object-Oriented

Programming (OOP)
Lecture No. 15
Composition
Conceptual notation:

Student
gpa : float
rollNo : int String
name : String string : char *
Student(char * = NULL, int = 0,
float = 0.0); String()
Student(const Student &) SetString(char *) : void
GetName() const : String GetString() const : const char *
GetNamePtr() const : const char * ~String()
SetName(char *) : void …
~Student()

Composition
Main Function:

int main(){
Student aStudent("Fakhir", 899,
3.1);
cout << endl;
cout << “Name:”
<< aStudent.GetNamePtr()
<< endl;
return 0;
}
Composition
►Output:
Constructor::String..
Constructor::Student..

Name: Fakhir
Destructor::Student..
Destructor::String..
Composition
Student::Student(char * n,
int roll, float g){
cout <<"Constructor::
Student..\n";
name.SetString(n);
rollNumber = roll;
gpa = g;
}
Composition
►To assign meaningful values to the
object, the function SetString is
called explicitly in the constructor
►This is an overhead
►Sub-object name in the student class
can be initialized using the
constructor
►“Member initialization list” syntax is
used
Composition
►Add an overloaded constructor to
the String class defined above:

class String{
char *ptr;
public:
String(); //String(char * = NULL);
String(char *);
String(const String &);
void SetName(char *);
~String();

};
Composition
String::String(char * str){
if(str != NULL){
ptr = new char[strlen(str)
+1];
strcpy(ptr, str);
}
else ptr = NULL;
cout << "Overloaded
Constructor::String..\
n";
}
Composition
►Student class is modified as
follows:
class Student{
private:
float gpa;
int rollNumber;
String name;
public:

Student(char *=NULL, int=0,
float=0.0);
};
Composition
►Student class
continued:
Student::Student(char * n,int roll,
float g): name(n){
cout << "Constructor::Student..\n";
rollNumber = roll;
gpa = g;
}
Composition
Main Function:

int main(){
Student aStudent("Fakhir", 899,
3.1);
cout << endl;
cout << “Name:”
<< aStudent.GetNamePtr()
<< endl;
return 0;
}
Composition
►Output:
Overloaded Constructor::String..
Constructor::Student..

Name: Fakhir
Destructor::Student..
Destructor::String..
Composition
Now consider the following case:
String
name: char *
Student
String()
… String(char *)
name : String ~String()
birthDate : Date …

Student()
Student( char *, Date
const Date &, int, float)
SetName(char *) : void day: int
GetName() : char * Month: int
~Student() year: int
… …
Date()
Date(int,int,int)
Date(const Date &)

Composition
►Student class is modified as
follows:
class Student{
private:

Date birthDate;
String name;
public:
Student(char *, const Date &, int,
float);
~Student();

};
Composition
►Student class continued:
Student::Student(char * n, const Date & d,
int roll, flaot g): name(n),birthDate(d){
cout << "Constructor::Student..\n";
rollNumber = roll;
gpa = g;
}

Student::~Student(){
cout << "Destructor::Student..\n";
}
Composition
►Main function:

int main(){
Date _date(31, 12, 1982);
Student aStudent("Fakhir",
_date,899,3.5);
return 0;
}
Composition
►Output:

Overloaded Constructor::Date..
Copy Constructor::Date..
Overloaded Constructor::String..
Constructor::Student..
Destructor::Student..
Destructor::String..
Destructor::Date..
Destructor::Date..
Aggregation
Composition vs.
Aggregation
►Aggregation is a weak
relationship
Room Chair
area : float …
chairs[50]:Chair *
Chair()
DoSomething() : void
Room(char *, int)
FoldChair() : bool
~Room()
UnFoldChair() : bool
FoldChair(int) : bool
~Chair()


Aggregation
►In aggregation, a pointer or
reference to an object is created
inside a class
►The sub-object has a life that is
NOT dependant on the life of its
master class
►e.g:
Chairs can be moved inside or outside at
anytime
When Room is destroyed, the chairs may
or may not be destroyed
Aggregation
class Room{
private:
float area;
Chair * chairs[50];
Public:
Room();
void AddChair(Chair *, int chairNo);
Chair * GetChair(int chairNo);
bool FoldChair(int chairNo);

};
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;
}
Aggregation
Chair * 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;
}
Aggregation
int main(){
Chair ch1;
{
Room r1;
r1.AddChair(&ch1, 1);
r1.FoldChair(1);
}
ch1.UnFoldChair(1);
return 0;
}
Friend
Functions
Consider the following class:

class X{
private:
int a, b;
public:
void MemberFunction();

}
Friend
Functions
Global function:

void DoSomething(X obj){


obj.a = 3; //Error
obj.b = 4; //Error
}
Friend
►In Functions
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);
}
►Nowthe function DoSomething can access data
members of class X
Friend
Functions
►Prototypes of friend
functions appear in the
class definition
►Butfriend functions are
NOT member functions
Friend
►FriendFunctions
functions can be placed anywhere
in the class without any effect
►Access specifiers don’t affect friend
functions or classes
class X{
...
private:
friend void DoSomething(X);
public:
friend void DoAnything(X);
...
};
Friend
► Functions
While the definition of the friend
function is:

void DoSomething(X obj){


obj.a = 3; // No
Error
obj.b = 4; // No
Error

}
►friend keyword is not given in
definition
Friend
Functions
►Ifkeyword friend is used in the
function definition, it’s a syntax error

//Error…

friend void DoSomething(X obj){



}
Friend Classes
•Similarly, one class can also be made
friend of another class:
class X{
friend class Y;

};
•Member functions of class Y can access
private data members of class X
Friend Classes
•Example:

class X{
friend class Y;
private:
int x_var1, x_var2;
...
};
Friend Classes
class Y{
private:
int y_var1, y_var2;
X objX;
public:
void setX(){
objX.x_var1 = 1;
}
};
Friend Classes
int main(){
Y objY;
objY.setX();
return 0;
}

You might also like