











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The implementation of the student class using composition, where a string data member is used to store the student's name. The constructor, destructor, and member functions of the student and string classes, as well as the main function that demonstrates the usage of the student class.
Typology: Slides
1 / 19
This page cannot be seen from the preview
Don't miss anything!












*gpa : floatrollNo : intname : char Student(char * = NULL, int = 0,
float = 0.0);
**Student(const Student &)GetName() const : const char SetName(char ) : void~Student()…
Student
_name,
int
roll, float
g){
cout
"Constructor::Student..\n"; if^ (!_name){ name
new char[strlen(_name)+1];
strcpy(name,_name); } else
name
rollNumber
roll;
gpa
g;
}
Student::Student(const
Student
st){
if(str.name
name
new char[strlen(st.name)
strcpy(name,
st.name);
} else
name
rollNumber
st.roll;
gpa
st.g; }
Creating objects of one class insideanother class
Bird has a beak Student has a name
**String()SetString(char ) : voidGetString() const : const char ~String()…
gpa : floatrollNo : intname : StringStudent(char * = NULL, int = 0,
float = 0.0);
**Student(const Student &)GetName() const : StringGetNamePtr() const : const char SetName(char ) : void~Student()…
Student
**string : char ***
String
String::String(){
cout << "Constructor::String..\n";ptr = NULL; } String::String(const String & str){
if(str.ptr != NULL){string = new char[strlen(str.ptr)+1];
strcpy(ptr, str.ptr);}
void String::SetString(char * str){
if(ptr != NULL){
delete [] ptr;ptr = NULL; } if(str != NULL){
ptr = new
char[strlen(str)+1];
strcpy(ptr, str); } }
class
Student{ private:float
gpa; int rollNumber; String
name; public:Student(char*
int=0,float=0.0);
Student(const
Student
void
SetName(const
char
String
GetName()
const;
const
char
GetNamePtr
const
~Student();… };
void Student::SetName(const char * n){
name.SetString(n); } Student::~Student(){
cout <<"Destructor::Student..\n"; }