You are on page 1of 44

Object Oriented Paradigm

Inam Qadir
Composition
Composition
Composition

 An object may be composed of other small objects.


 The relationships between the “part” and “Whole” object is known as
composition.
 Composition is represented by a line with a filled diamond toward the
composer object.
Example: Composition of Ali

Head

Arms Ali Leg

Body
Composition

 Composition is a stronger relationship, because:


 Composed object becomes a part of the composer.
 Composed object can’t exist independently.
Example: Ali is Stronger

 Ali is made up of different body parts.


 They can’t exist independent of Ali.
Composition

 Consider the following implementation of the student.

Student
Gpa: float
Rollno: int
Name: char *
Student(char *=nullptr, int=0,
float=0.0);
Student(const Student &);
Setname(char *): void
Getname():const char *
~Student();
Composition

Class Student
{
Private:
Float gpa;
Char * name;
Int rollno;
Public:
Student(char * =nullptr, int =0, float=0.0);
Student(const Student &);
Void setname(char *);
Const char * Getname()const;
~Student();
};
Composition

Student::Student(char * n, int roll, float g)


{
cout<<“Constructor::Student..\n”;
name=new char[strlen(n)+1];
strcpy(name,n);
rollno=roll;
gpa=g;
}
Composition

Student::Student(const Student & st)


{
If(st.name!=nullptr)
{
name=new char[strlen(st.name)+1];
strcpy(name,st.name);
}
else
name=nullptr;
rollno=st.rollno;
gpa=st.gpa;
}
Composition

const char* Student::getname()


{
return name;
}
Student::~Student()
{
delete [] name;
}
Composition

 Consider the following implementation of the student.

Student
Gpa: float
Rollno: int String
Name: char *
String: char *
Student(char *=nullptr, int=0,
float=0.0); String();
Student(const Student &); SetString(char *): void
Setname(char *): void Getstringptr()const:const char *
Getname():const char * ~String();
~Student();
Composition

Class String
{
Private:
char * ptr;
Public:
String();
String(const String & str);
void setstring(char * str);
const char * Getstring() const;
~String();
};
Composition

String::String()
{
ptr=nullptr;
}
Composition

String::String(const String & str)


{
If(str.ptr!=nullptr)
{
ptr=new char[strlen(str.ptr)+1];
strcpy(ptr,str.ptr);
}
else
ptr=nullptr;
}
Composition

void String::setstring(char * str)


{
if(ptr!=null)
{
delete[] ptr;
ptr=nullptr;
}
If(str!=nullptr)
{
ptr=new char[strlen(str)+1];
Strcpy(ptr,str);
}
}
Composition

const char * String:: getString() const


{
return ptr;
}
String::~String()
{
delete[] ptr;
cout<<“Destructor ::String..\n”;
}
Composition

Class Student
{
Private:
Float gpa;
Int rollno;
String name;
Public:
Student(char * =nullptr, int =0, float=0.0);
Student(const Student &);
Void setname(const char *);
String Getname() const;
Const char * GetnamePtr()const;
~Student();
};
Composition

Student::Student(char * n, int roll, float g)


{
cout<<“Constructor::Student..\n”;
name.setstring(n);
Rollno=roll;
Gpa=g;
}
Composition

Student::Student(const Student & st)


{
name.setname(st.name.getString());
gpa=st.gpa;
rollno=st.rollno;
}
const char * Student::Getnameptr()const
{
return name.Getstring();
}
Composition

Const char* Student::setname(const char * n)


{
name.setstring(n);
}
Student::~Student()
{
cout<<“Destructor :: Student … \n”;
}
Composition

 Main function:
void main()
{
Student aStudent(“Fakhir”, 899);
cout<<“\n Name:”<<aStudent.Getnameptr()<<“\n”;
}
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);
Rollno=roll;
Gpa=g;
}
Composition

 output
Constructor:: String;
Constructor::Student;
Name: Fakhir
Destructor: Student
Destructor: String
Composition

 Constructor of the sub object are always called before the constructors of the
master class.
Example:
 Constructor of the sub object name is called before the constructor of student.
Composition

 Conceptual notation.

Student
Gpa: float
Rollno: int String
Name: String
String: char *
Student(char *=nullptr, int=0,
float=0.0); String();
Student(const Student &); SetString(char *): void
Setname(char *): void Getstringptr()const:const char *
Getname():const char * ~String();
~Student();
Composition

 Main function:
void main()
{
Student aStudent(“Fakhir”, 899,3.14);
cout<<“\n Name:”<<aStudent.Getnameptr()<<“\n”;
}
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);
Rollno=roll;
Gpa=g;
}
Composition

 To assign meaning full 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

 Added an overloaded constructor to the string class define above:


Class String
{
Private:
char * ptr;
Public:
String();
String(char *);
String(const String & str);
void setstring(char * str);
const char * Getstring() const;
~String();
};
Composition

String::String(char *str)
{
If(str!=nullptr)
{
ptr=new char[strlen(str)+1];
strcpy(ptr,str);
}
else
ptr=nullptr;
Cout<<“overloaded constructor :: String \n”;
}
Composition

Class Student
{
Private:
Float gpa;
Int rollno;
String name;
Public:
Student(char * =nullptr, int =0, float=0.0);

};
Composition

 Student class continued.


Student::Student(char * n, int roll, float g):name(n)
{
cout<<“Constructor::Student..\n”;
Rollno=roll;
Gpa=g;
}
Composition

 Main function:
void main()
{
Student aStudent(“Fakhir”, 899,3.14);
cout<<“\n Name:”<<aStudent.Getnameptr()<<“\n”;
}
Composition

 output
Overloaded Constructor:: String;
Constructor::Student;
Name: Fakhir
Destructor: Student
Destructor: String
Composition

 Conceptual notation. String


String: char *
Student
String();
… String(char *);
Name: String Getstringptr()const: const char *
DoB: Date ~String();
Student(char *=nullptr, int=0,
float=0.0); Date
Student(const Student &);
Setname(char *): void Day: int
Getname():const char * Moth: int
~Student(); Year: int

Date();
Date(int,int,int)
Date(const Date &);

Composition

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, float g):name(n),birthdate(d)
{
cout<<“Constructor::Student..\n”;
Rollno=roll;
Gpa=g;
}
Student::~Student()
{
cout<<“constructor of the :: Student”;
}
Composition

 Conceptual notation. String


String: char *
Student
String();
… String(char *);
Name: String Getstringptr()const: const char *
DoB: Date ~String();
Student(char *=nullptr, int=0,
float=0.0); Date
Student(const Student &);
Setname(char *): void Day: int
Getname():const char * Moth: int
~Student(); Year: int

Date();
Date(int,int,int)
Date(const Date &);

Composition

 Main function:
void main()
{
Date _date(10,11,2011)
Student aStudent(“Fakhir”,_date, 899,3.14);
}
Composition

 Output
Overloaded Constructor:: Date;
Copy Constructor:: Date;
Overloaded Constructor:: String;
Constructor::Student;
Destructor: Student
Destructor: String
Destructor: Date
Destructor: Date
Thanks

You might also like