Professional Documents
Culture Documents
Inam Qadir
Composition
Composition
Composition
Head
Body
Composition
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
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
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
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
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
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
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
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
Class Student
{
Private:
…
Date birthdate;
String name;
Public:
Student(char * , const Date &, int, float);
~Student();
…
};
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