Upload
ahsan161
View
33
Download
0
Tags:
Embed Size (px)
DESCRIPTION
classes in c++
Citation preview
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
Page 2 of 20
Classes
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 3 of 20
Classes Implementation
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 4 of 20
Classes Implementation
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
#include
#include
#include
using namespace std;
const int MAXNAMELEN = 51;
const int MAXSCHOOLLEN = 101;
class Student
{ // ********* Private data member **************
private:
char school[MAXSCHOOLLEN];
char name[MAXNAMELEN];
int id;
int rank;
double cumulativeGpa;
public:
void setSchool(char inSchool[]); // accessor methods
void setName(char inName[]);
void setId(int inId);
void setRank(int inRank);
void setGpa(double inGpa);
void displayData();
};
int main( )
{
Student stu1;
char stuName[MAXNAMELEN], schName[MAXSCHOOLLEN];
int idNum, rank;
double gpa;
cout rank;
cout > gpa;
stu1.setName(stuName);
stu1.setSchool(schName);
stu1.setId(idNum);
stu1.setRank(rank);
stu1.setGpa(gpa);
stu1.displayData();
cout
Page 5 of 20
Classes Implementation
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 6 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
*/
// a Complete example by declaring, using structs as well as data members.
#include
#include
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
Page 7 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 8 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
cout
Page 9 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 10 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
/*
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");
Page 11 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 12 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
#include
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();
Page 13 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
//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)
Page 14 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
----------------------------------
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
#include
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");
}
Page 15 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
//--------------------------------------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
Page 16 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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)
{
Page 17 of 20
Classes Constructors & Destructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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
Page 18 of 20
Arrays of Class Objects & Constructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
*/
Arrays of Class Objects & Constructors A cat class example which demonstrates that how to create the array of objects.
#include #include 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
Page 19 of 20
Arrays of Class Objects & Constructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
morris is orange tom is black_and_white and speaks meow tom is black_and_white and speaks meow tom is black_and_white and speaks meow tom is black_and_white and speaks meow tom is black_and_white and speaks meow Press any key to continue . . . */
#include using namespace std; const int MAX =100; class Details { private: int fscMarks; int rollNumber; public: void inputData( ) { cout > rollNumber; cout > fscMarks; } void printData( ) { cout
Page 20 of 20
Arrays of Class Objects & Constructors
CS103-Computer Programming By: Mr. Najeeb -Ur-Rehman
Object Oriented Programming (OOP)
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 . . . */