Object-Oriented Programming: Student Class with String Data Member, Slides of Object Oriented Programming

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

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 14
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Object-Oriented Programming: Student Class with String Data Member and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 14

Composition

Consider the following implementationof the student class:

*gpa : floatrollNo : intname : char Student(char * = NULL, int = 0,

float = 0.0);

**Student(const Student &)GetName() const : const char SetName(char ) : void~Student()…

Student

Composition Student::Student(char

*^

_name,

int

roll, float

g){

cout

"Constructor::Student..\n"; if^ (!_name){ name

=^

new char[strlen(_name)+1];

strcpy(name,_name); } else

name

=^

NULL;

rollNumber

=^

roll;

gpa

=^

g;

}

Composition

Student::Student(const

Student

&^

st){

if(str.name

NULL){

name

=^

new char[strlen(st.name)

+^

1];

strcpy(name,

st.name);

} else

name

=^

NULL;

rollNumber

=^

st.roll;

gpa

=^

st.g; }

Composition

►C++: “

its all about code reuse”

►Composition:

Creating objects of one class insideanother class

Has a

”^ relationship:

Bird has a beak Student has a name

Composition

Conceptual notation:

**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

Composition

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);}

Composition

void String::SetString(char * str){

if(ptr != NULL){

delete [] ptr;ptr = NULL; } if(str != NULL){

ptr = new

char[strlen(str)+1];

strcpy(ptr, str); } }

Composition

class

Student{ private:float

gpa; int rollNumber; String

name; public:Student(char*

=NULL,

int=0,float=0.0);

Student(const

Student

void

SetName(const

char

String

GetName()

const;

const

char

*^

GetNamePtr

const

~Student();… };

Composition

Student Student(char * _name,

int roll,

float g){

cout <<"Constructor::Student..\n";name.SetString(_name);rollNumber = roll;gpa = g;

Composition

void Student::SetName(const char * n){

name.SetString(n); } Student::~Student(){

cout <<"Destructor::Student..\n"; }

Composition

Main Function: void main(){

Student aStudent("Fakhir", 899,

cout << endl;cout << “Name:”

<< aStudent.GetNamePtr()<< “\n”;

Composition

►Constructors of the sub-objectsare always executed before theconstructors of the master class ►Example:

Constructor for the sub-object name

is executed before the

constructor of

Student