Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

CS103-Computer Programming

Object Oriented Programming (OOP)


Objectives:
Classes
Defining Classes and Member Functions
Member Function Definition
The Dot Operator And The Scope Resolution Operator
A Class is a Full-Fledged Type
Public & Private Members
Classes Implementation
Classes Constructors & Destructors
Default Constructor
Parameterized Constructor
Overloaded Constructor
Constructor with Default Parameters
Copy Constructor
Arrays of Class Objects & Constructors

Object Oriented Programming (OOP)

Page 2 of 20
Classes

Getting Started

Classes
A class is basically a structure with member functions as well as member data. Classes are central to the
programming methodology known as object-oriented programming.
Defining Classes and Member Functions
A class is a type that is similar to a structure type, but a class type normally has member functions as
well as member variables
Member Function Definition
A member function is defined similar to any other function except that the class name and the scope
resolution operator, ::, are given in the function heading.
Syntax
Note that the member variable (month and day) are not preceded by and object name and do not when
they occur in member function definition.
In the function definition for a member function, you can use the names of all members of the class
(both the data members and the function members) without using the dot operator.
The Dot Operator and the Scope Resolution Operator
Both the dot operator and the scope resolution operator used with member names to specify of what
thing they are member.
A Class is a Full-Fledged Type
A class is a type just like int and double. You can have variables of a class type, you can have parameter
of class type, a function can return a value of a class type, and more generally, you can use a class type
like any other type.
Public & Private Members
There is not universal agreement about whether the public members should be listed first or the private
members should be listed first. The majority seem to prefer listing the public members first. This allows
for easy viewing of the portions programmers using the class actually get to use. You can make your own
decision on what you wish to place first, but the examples in the book will go along with the majority
and list the public members first, before the private members.
In one sense, C++ seems to favor private member first. If the first group of members has neither the
public: nor the private: specifier, then members of that group will automatically be private. You will see
this default behavior use in code and should Familiar with it. However, we will not use it in this book.

Classes Implementation
A class is a combination of data and functions joined together to form an object type. The object type
defines the operations that are necessary to manipulate the object. By creating a class, we can take

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 3 of 20
Classes Implementation

advantage of data hiding, which is very important in programming. Data hiding is physically located data
in the class (inside the class object type) that is hidden from the "outside", which is everything in the
program not defined in the class type. The data can only be accessed or changed by functions that are
apart of the class. These member functions that provide access to the hidden data are called accessor
methods. The declaration of a class is similar to that of a structure, except that usually there will be
member functions along with data members. There will also be some type of syntax for distinguishing
members that are available for public use and ones that will be kept private.
The best way to learn classes is by studying a simple class object type. The following is a very simple
class object type:
class myClass
{
private :
int data1;
public :
void setData( int d )
{
data1 = d;

void displayData( )
{
cout << endl << "Data is " << data1;

};

#include <iostream>
using namespace std;
// ===================== class example
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area (void) {return (x*y);}
};

========================

void CRectangle::set_values (int a, int b)


{
x = a;
y = b;
}
int main ()
{
CRectangle rect, rectb;
rect.set_values (3,4);
rectb.set_values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
system("PAUSE");
}
/*
rect area: 12
rectb area: 30
Press any key to continue . . .
*/

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 4 of 20
Classes Implementation

#include <stdlib.h>
#include <iostream>
#include <string.h>
using namespace std;
const int MAXNAMELEN = 51;
const int MAXSCHOOLLEN = 101;
class Student
{
private:

// *********

Private data member **************

char school[MAXSCHOOLLEN];
char name[MAXNAMELEN];
int id;
int rank;
double cumulativeGpa;
public:
void
void
void
void
void

setSchool(char inSchool[]);
setName(char inName[]);
setId(int inId);
setRank(int inRank);
setGpa(double inGpa);

// accessor methods

void displayData();
};
int main( )
{
Student stu1;
char stuName[MAXNAMELEN], schName[MAXSCHOOLLEN];
int idNum, rank;
double gpa;
cout << "Enter first student's name: ";
cin.get(stuName, MAXNAMELEN);
cin.ignore(80, '\n');
cout << "Enter first student's school: ";
cin.get(schName, MAXSCHOOLLEN);
cin.ignore(80, '\n');
cout << "Enter first student's identification number: ";
cin >> idNum;
cout << "Enter first student's rank: ";
cin >> rank;
cout << "Enter first student's cumulative gpa: ";
cin >> gpa;
stu1.setName(stuName);
stu1.setSchool(schName);
stu1.setId(idNum);
stu1.setRank(rank);
stu1.setGpa(gpa);
stu1.displayData();
cout << endl << endl;
system("PAUSE");

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 5 of 20
Classes Implementation

return EXIT_SUCCESS;
}

void Student :: setSchool(char inSchool[])


{
strncpy(school, inSchool, MAXSCHOOLLEN);
}
void Student :: setName(char inName[])
{
strncpy(name, inName, MAXNAMELEN);
}
void Student :: setId(int inId)
{
id = inId;
}
void Student :: setRank(int inRank)
{
rank = inRank;
}
void Student :: setGpa(double inGpa)
{
cumulativeGpa = inGpa;
}
void Student :: displayData()
{
cout << "\n\nSTUDENT DATA\n\n";
cout << "
NAME...........: " << name << endl;
cout << "
SCHOOL.........: " << school << endl;
cout << "
ID.............: " << id << endl;
cout << "
RANK...........: " << rank << endl;
cout << "
CUMULATIVE GPA.: " << cumulativeGpa << endl << endl;
}
/*
Enter first student's name: Najeeb-Ur-Rehman
Enter first student's school: FAST-NUCES,Pwr
Enter first student's identification number: 060132
Enter first student's rank: 1
Enter first student's cumulative gpa: 3.67
STUDENT DATA
NAME...........:
SCHOOL.........:
ID.............:
RANK...........:
CUMULATIVE GPA.:

Najeeb-Ur-Rehman
FAST-NUCES,Pwr
60132
1
3.67

Press any key to continue . . .

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 6 of 20

Classes Constructors & Destructors


*/

// a Complete example by declaring, using structs as well as data members.


#include <iostream>
#include <conio.h>
using namespace std;
class CDAccountV1
{
double balance;
double interestRate;
int term;
};
void main()
{
CDAccountV1 a,b;
a.balance=500.0;
a.interestRate=0.10;
a.term=1;
cout<<" Balance of A : "<< a.balance
<<"\t Iterest Rate of A : "<<a.interestRate
<<"\t Term of A : "<<a.term<<endl;
b.balance=a.balance*10.0;
b.interestRate=a.interestRate*2;
b.term=a.term+2;;
cout<<" Balance of B : "<< b.balance
<<"\t Iterest Rate of B : "<<b.interestRate
<<"\t Term of B : "<<b.term<<endl;
system("PAUSE");
}
/*
Balance of A : 500
Iterest Rate of A : 0.1
Term of A : 1
Balance of B : 5000
Iterest Rate of B : 0.2
Term of B : 3
Press any key to continue . . .
*/

Classes Constructors & Destructors


Default Constructor
Given example demonstrates the concept of default constructor.
#include <string>
#include <iostream>
using namespace std;
class ClassRoom {
private:
int roomID;
int numberOfChairs;
char boardType; // C for chalk and M for marker
string multimedia;
string remarks;
public:
ClassRoom();

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 7 of 20

Classes Constructors & Destructors


void setroomID(int);
void setnumberOfChairs(int);
void setboardType(char);
void setmultimedia(string);
void setremarks(string);
int
getroomID();
int
getnumberOfChairs();
char getboardType();
string
getmultimedia();
string
getremarks();
void display();
};
void main ()
{
ClassRoom CR0;
CR0.display();
system("PAUSE");
}
//--------------------------------------Constructors
ClassRoom::ClassRoom()
{
this->roomID=0;
this->numberOfChairs=0;
this->boardType='M';
this->multimedia="New";
this->remarks="Default Constructor";
}
//--------------------------------------Getter Functions
int ClassRoom::getroomID()
{
return this->roomID;
}
int ClassRoom::getnumberOfChairs()
{
return this->numberOfChairs;
}
char ClassRoom::getboardType()
{
return (this->boardType);
}
string ClassRoom::getmultimedia()
{
return this->multimedia;
}
string ClassRoom::getremarks()
{
return this->remarks;
}
//------------------------------------Printing Functions
void ClassRoom::display()
{
cout<<"Room ID \t: "<<this->getroomID()<<endl;
cout<<"N.O. Chairs \t: "<<this->getnumberOfChairs()<<endl;
cout<<"Board Type \t: "<<this->getboardType()<<endl;
cout<<"Multimedia \t: "<<this->getmultimedia()<<endl;

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 8 of 20

Classes Constructors & Destructors


cout<<"Remarks \t: \n"<<this->getremarks()<<endl;
cout<<"----------------------------------"<<endl;
}
/*
Room ID
: 0
N.O. Chairs
: 0
Board Type
: M
Multimedia
: New
Remarks
:
Default Constructor
---------------------------------Press any key to continue . . .
*/

Parameterized Constructor
Given example demonstrates the concept of parameterized constructor.
#include <string>
#include <iostream>
using namespace std;
class ClassRoom {
private:
int roomID;
int numberOfChairs;
char boardType; // C for chalk and M for marker
string multimedia;
string remarks;
public:
ClassRoom(int id,int NOC,char,string mul, string rmks);
void setroomID(int);
void setnumberOfChairs(int);
void setboardType(char);
void setmultimedia(string);
void setremarks(string);
int
getroomID();
int
getnumberOfChairs();
char getboardType();
string
getmultimedia();
string
getremarks();
void display();
};
void main ()
{
ClassRoom CR(11,25,'M',"Repairing..","Remarks added from Main");
CR.display();
system("PAUSE");
}
//--------------------------------------Constructor
ClassRoom::ClassRoom(int id,int NOC,char c,string mul, string remks)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 9 of 20

Classes Constructors & Destructors


this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks(remks+"\nConstructor with All(5)Parameters
(ID,NOC,BoardType,Multimedia,Remarks)");
}
//---------------------------------------Setter Functions
void ClassRoom::setroomID(int id)
{
this->roomID=id;
}
void ClassRoom::setnumberOfChairs(int NOC)
{
this->numberOfChairs=NOC;
}
void ClassRoom::setboardType(char c)
{
this->boardType=c;
}
void ClassRoom::setmultimedia(string str)
{
this->multimedia=str;
}
void ClassRoom::setremarks(string str)
{
this->remarks=str;
}
//--------------------------------------Getter Functions
int ClassRoom::getroomID()
{
return this->roomID;
}
int ClassRoom::getnumberOfChairs()
{
return this->numberOfChairs;
}
char ClassRoom::getboardType()
{
return (this->boardType);
}
string ClassRoom::getmultimedia()
{
return this->multimedia;
}
string ClassRoom::getremarks()
{
return this->remarks;
}
//------------------------------------Printing Functions
void ClassRoom::display()
{
cout<<"Room ID \t: "<<this->getroomID()<<endl;
cout<<"N.O. Chairs \t: "<<this->getnumberOfChairs()<<endl;
cout<<"Board Type \t: "<<this->getboardType()<<endl;
cout<<"Multimedia \t: "<<this->getmultimedia()<<endl;
cout<<"Remarks \t: \n"<<this->getremarks()<<endl;
cout<<"----------------------------------"<<endl;
}

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 10 of 20

Classes Constructors & Destructors


/*
Room ID
: 11
N.O. Chairs
: 25
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Remarks added from Main
Constructor with All(5)Parameters (ID,NOC,BoardType,Multimedia,Remarks)
---------------------------------Press any key to continue . . .
*/

Overloaded Constructor
Given example demonstrates the concept of overloaded constructor.
class ClassRoom {
private:
// Other class data members as in above example
public:
ClassRoom();
ClassRoom(int id);
ClassRoom(int id,int NOC);
ClassRoom(int id,int NOC,char c);
ClassRoom(int id,int NOC,char,string mul);
ClassRoom(int id,int NOC,char,string mul, string rmks);
};
void main ()
{
ClassRoom CR0,CR1(1),CR2(2,30),CR3(3,35,'M'),
CR4(4,40,'M',"Repairing.."),CR5(5,40,'M',"Repairing..","Remarks
added from Main");
CR0.display();
CR1.display();
CR2.display();
CR3.display();
CR4.display();
CR5.display();
system("PAUSE");
}
//--------------------------------------Constructors
ClassRoom::ClassRoom()
{
this->roomID=0;
this->numberOfChairs=0;
this->boardType='M';
this->multimedia="New";
this->remarks="Default Constructor";
}
ClassRoom::ClassRoom(int id)
{
this->roomID=id;
this->setnumberOfChairs(0);
this->setboardType('M');
this->setmultimedia("New");

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 11 of 20

Classes Constructors & Destructors


this->remarks="Constructor with 1-Parameter (ID) ";
}
ClassRoom::ClassRoom(int id,int NOC)
{
this->roomID=id;
this->numberOfChairs=NOC;
this->setboardType('M');
this->setmultimedia("New");
this->remarks="Constructor with 2-Parameters (ID,NOC) ";
}
ClassRoom::ClassRoom(int id,int NOC,char c)
{
this->roomID=id;
this->numberOfChairs=NOC;
this->boardType=c;
this->setmultimedia("New");
this->remarks="Constructor with 3-Parameters (ID,NOC,BoardType)
";
}
ClassRoom::ClassRoom(int id,int NOC,char c,string mul)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;
this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks("Constructor with 4-Parameters
(ID,NOC,BoardType,Multimedia)");
}
ClassRoom::ClassRoom(int id,int NOC,char c,string mul, string remks)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;
this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks(remks+"\nConstructor with All(5)Parameters
(ID,NOC,BoardType,Multimedia,Remarks)");
}
/*
Room ID
: 0
N.O. Chairs
: 0
Board Type
: M
Multimedia
: New
Remarks
:
Default Constructor
---------------------------------Room ID
: 1
N.O. Chairs
: 0
Board Type
: M
Multimedia
: New
Remarks
:
Constructor with 1-Parameter (ID)
---------------------------------Room ID
: 2
N.O. Chairs
: 30
Board Type
: M
Multimedia
: New

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 12 of 20

Classes Constructors & Destructors


Remarks
:
Constructor with 2-Parameters (ID,NOC)
---------------------------------Room ID
: 3
N.O. Chairs
: 35
Board Type
: M
Multimedia
: New
Remarks
:
Constructor with 3-Parameters (ID,NOC,BoardType)
---------------------------------Room ID
: 4
N.O. Chairs
: 40
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Constructor with 4-Parameters (ID,NOC,BoardType,Multimedia)
---------------------------------Room ID
: 5
N.O. Chairs
: 40
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Remarks added from Main
Constructor with All(5)Parameters (ID,NOC,BoardType,Multimedia,Remarks)
---------------------------------Press any key to continue . . .
*/

Constructor with Default Parameters


Given example demonstrates the concept of constructor with default values.
#include <string>
#include <iostream>
using namespace std;
class ClassRoom {
private:
int roomID;
int numberOfChairs;
char boardType; // C for chalk and M for marker
string multimedia;
string remarks;
public:
ClassRoom(int id=0,int NOC=25,char c='C',string mul="New",string
rmks="Defaut Value");
void setroomID(int);
void setnumberOfChairs(int);
void setboardType(char);
void setmultimedia(string);
void setremarks(string);
int
getroomID();
int
getnumberOfChairs();
char getboardType();
string
getmultimedia();
string
getremarks();

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 13 of 20

Classes Constructors & Destructors


void display();
};
void main ()
{
ClassRoom CR0,CR1(1),CR2(2,30),CR3(3,35,'M'),
CR4(4,40,'M',"Repairing.."),CR5(5,40,'M',"Repairing..","Remarks
added from Main");
CR0.display();
CR1.display();
CR2.display();
CR3.display();
CR4.display();
CR5.display();
system("PAUSE");
}
//--------------------------------------Constructor
ClassRoom::ClassRoom(int id,int NOC,char c,string mul, string remks)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;
this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks(remks);
}
// All Setter & Getter Functions Deffinitions <Code here all the setter and
getter functions>
//Printing Functions <code here all printing functions>
/*
Room ID
: 0
N.O. Chairs
: 0
Board Type
: M
Multimedia
: New
Remarks
:
Default Constructor
---------------------------------Room ID
: 1
N.O. Chairs
: 0
Board Type
: M
Multimedia
: New
Remarks
:
Constructor with 1-Parameter (ID)
---------------------------------Room ID
: 2
N.O. Chairs
: 30
Board Type
: M
Multimedia
: New
Remarks
:
Constructor with 2-Parameters (ID,NOC)
---------------------------------Room ID
: 3
N.O. Chairs
: 35
Board Type
: M
Multimedia
: New
Remarks
:
Constructor with 3-Parameters (ID,NOC,BoardType)

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 14 of 20

Classes Constructors & Destructors


---------------------------------Room ID
: 4
N.O. Chairs
: 40
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Constructor with 4-Parameters (ID,NOC,BoardType,Multimedia)
---------------------------------Room ID
: 5
N.O. Chairs
: 40
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Remarks added from Main
Constructor with All(5)Parameters (ID,NOC,BoardType,Multimedia,Remarks)
---------------------------------Press any key to continue . . .
*/

#include <string>
#include <iostream>
using namespace std;
class ClassRoom {
private:
//All data members of this calls, for reference see the above example
public:
ClassRoom(int id=0,int NOC=25,char c='C',string mul="New",string
rmks="Defaut Value");
~ClassRoom();
//All setter,getter and prinitng functions prototyping statements (see
above example for reference)
};
void main ()
{
ClassRoom CR0;
{
ClassRoom CR1(1);
{
ClassRoom CR2(2,30);
{
ClassRoom CR3(3,35,'M');
{
ClassRoom CR4(4,40,'M',"Repairing..");
{
ClassRoom
CR5(5,40,'M',"Repairing..","Remarks added from Main");
}
}
}
}
}
system("PAUSE");
}

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 15 of 20

Classes Constructors & Destructors


//--------------------------------------Constructor
ClassRoom::ClassRoom(int id,int NOC,char c,string mul, string remks)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;
this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks(remks);
}
//--------------------------------------Destructor
ClassRoom::~ClassRoom()
{
cout<<" Destructor of Room < "<<this->roomID<<" >is called "<<endl;
}
//---------------------------------------Setter Functions
/*
Destructor of Room < 5 >is called
Destructor of Room < 4 >is called
Destructor of Room < 3 >is called
Destructor of Room < 2 >is called
Destructor of Room < 1 >is called
Press any key to continue . . .
*/

Copy Constructor
Given example demonstrates the concept of copy constructor.
#include <string>
#include <iostream>
using namespace std;
class ClassRoom {
private:
int roomID;
int numberOfChairs;
char boardType; // C for chalk and M for marker
string multimedia;
string remarks;
public:
ClassRoom(int id=0,int NOC=25,char c='C',string mul="New",string
rmks="Defaut Value");
ClassRoom(ClassRoom);
ClassRoom copyMe();
void setroomID(int);
void setnumberOfChairs(int);
void setboardType(char);
void setmultimedia(string);
void setremarks(string);
int
getroomID();
int
getnumberOfChairs();
char getboardType();
string
getmultimedia();
string
getremarks();
void display();
};

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 16 of 20

Classes Constructors & Destructors


void main ()
{
ClassRoom CR0;
ClassRoom CR1(CR0);
CR0.display();
CR1.display();
CR1.display();
system("PAUSE");
}
//--------------------------------------Constructors
ClassRoom::ClassRoom(int id,int NOC,char c,string mul, string remks)
{
this->setroomID(id);
//this->roomID=id;
this->setnumberOfChairs(NOC); //this->numberOfChairs=NOC;
this->setboardType(c);
//this->boardType=c;
this->setmultimedia(mul);
//this->multimedia=str;
this->setremarks(remks);
}
ClassRoom::ClassRoom(ClassRoom CR)
{
this->roomID=CR.roomID;
this->numberOfChairs=CR.numberOfChairs;
this->boardType=CR.boardType;
this->multimedia=CR.multimedia;
this->remarks=CR.remarks+" Object Created by Copy Constructor";
}
//--------------------------------------Destructors
ClassRoom ClassRoom::copyMe()
{
ClassRoom temp;
temp.roomID=this->roomID;
temp.numberOfChairs=this->numberOfChairs;
temp.boardType=this->boardType;
temp.multimedia=this->multimedia;
temp.remarks=this->remarks+" After Copying Room ";
return temp;
}
//---------------------------------------Setter Functions
void ClassRoom::setroomID(int id)
{
this->roomID=id;
}
void ClassRoom::setnumberOfChairs(int NOC)
{
this->numberOfChairs=NOC;
}
void ClassRoom::setboardType(char c)
{
this->boardType=c;
}
void ClassRoom::setmultimedia(string str)
{
this->multimedia=str;
}
void ClassRoom::setremarks(string str)
{

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 17 of 20

Classes Constructors & Destructors


this->remarks=str;
}
//--------------------------------------Getter Functions
int ClassRoom::getroomID()
{
return this->roomID;
}
int ClassRoom::getnumberOfChairs()
{
return this->numberOfChairs;
}
char ClassRoom::getboardType()
{
return (this->boardType);
}
string ClassRoom::getmultimedia()
{
return this->multimedia;
}
string ClassRoom::getremarks()
{
return this->remarks;
}
//------------------------------------Printing Functions
void ClassRoom::display()
{
cout<<"Room ID \t: "<<this->getroomID()<<endl;
cout<<"N.O. Chairs \t: "<<this->getnumberOfChairs()<<endl;
cout<<"Board Type \t: "<<this->getboardType()<<endl;
cout<<"Multimedia \t: "<<this->getmultimedia()<<endl;
cout<<"Remarks \t: \n"<<this->getremarks()<<endl;
cout<<"----------------------------------"<<endl;
}
/*
Room ID
: 0
N.O. Chairs
: 25
Board Type
: C
Multimedia
: New
Remarks
:
Defaut Value
---------------------------------Room ID
: 11
N.O. Chairs
: 25
Board Type
: M
Multimedia
: Repairing..
Remarks
:
Remarks added from Main
---------------------------------Room ID
: 0
N.O. Chairs
: 25
Board Type
: C
Multimedia
: New
Remarks
:
Defaut Value After Copying Room
---------------------------------Press any key to continue . . .

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 18 of 20

Arrays of Class Objects & Constructors


*/

Arrays of Class Objects & Constructors


A cat class example which demonstrates that how to create the array of objects.
#include <iostream>
#include <string>
using namespace std;
class Cat {
public:
Cat(string name = "tom", string color = "black_and_white") : _name(name),
_color(color) {}
~Cat() {}
void setName(string name)
{
_name = name;
}
string getName()
{
return _name;
}
void setColor(string color)
{
_color = color;
}
string getColor()
{
return _color;
}
void speak()
{
cout << "meow" << endl;
}
private:
string _name;
string _color;
};
int main()
{
Cat cat1("morris","orange");
//Objects of automatic extent exist on stack.
Cat ct[5];
cout << cat1.getName() << " is " << cat1.getColor() << endl;
for(int i=0; i<5; i++)
{
cout<<ct[i].getName()<<" is "<<ct[i].getColor()<<" and speaks ";
ct[i].speak();
}
return 0;
}
/*
Sample Runs:

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 19 of 20

Arrays of Class Objects & Constructors


morris is orange
tom is black_and_white and speaks
tom is black_and_white and speaks
tom is black_and_white and speaks
tom is black_and_white and speaks
tom is black_and_white and speaks
Press any key to continue . . .
*/

meow
meow
meow
meow
meow

#include <iostream>
using namespace std;
const int MAX =100;
class Details
{
private:
int fscMarks;
int rollNumber;
public:
void inputData( )
{
cout << "\n Enter the Roll Number: ";
cin >> rollNumber;
cout << "\n Enter the FSc Marks: ";
cin >> fscMarks;
}
void printData( )
{
cout << "Student with Roll # :" << rollNumber<<
" have " << rollNumber << " marks in FS.\n";
}
};
void main()
{
Details det[MAX];
int n=0;
char ans;
do{
cout << "Enter the Employee Number # " <<
det[n++].inputData();
cout << "\n\t\tEnter another (y/n)?: " ;
cin >> ans;
} while ( ans != 'n' );

n+1;

for (int j=0; j<n; j++)


{
cout << "\nEmployee Number is:: " << j+1;
det[j].printData( );
}
system("PAUSE");
}
/*
Sample Run:
Enter the Employee Number # 1
Enter the Roll Number: 111

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

Object Oriented Programming (OOP)

Page 20 of 20

Arrays of Class Objects & Constructors

Enter the FSc Marks: 555


Enter another (y/n)?: y
Enter the Employee Number # 2
Enter the Roll Number: 222
Enter the FSc Marks: 666
Enter another (y/n)?: y
Enter the Employee Number # 3
Enter the Roll Number: 333
Enter the FSc Marks: 777
Enter another (y/n)?: y
Enter the Employee Number # 4
Enter the Roll Number: 444
Enter the FSc Marks: 888
Enter another (y/n)?: y
Enter the Employee Number # 5
Enter the Roll Number: 555
Enter the FSc Marks: 999
Enter another (y/n)?: n
Employee Number is:: 1Student with Roll # :111 have 111 marks in FS.
Employee Number is:: 2Student with Roll # :222 have 222 marks in FS.
Employee Number is:: 3Student with Roll # :333 have 333 marks in FS.
Employee Number is:: 4Student with Roll # :444 have 444 marks in FS.
Employee Number is:: 5Student with Roll # :555 have 555 marks in FS.
Press any key to continue . . .
*/

CS103-Computer Programming

By: Mr. Najeeb -Ur-Rehman

You might also like