Classes I PDF
Classes I PDF
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
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);}
};
========================
CS103-Computer Programming
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:
// *********
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
Page 5 of 20
Classes Implementation
return EXIT_SUCCESS;
}
Najeeb-Ur-Rehman
FAST-NUCES,Pwr
60132
1
3.67
CS103-Computer Programming
Page 6 of 20
CS103-Computer Programming
Page 7 of 20
CS103-Computer Programming
Page 8 of 20
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
Page 9 of 20
CS103-Computer Programming
Page 10 of 20
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
Page 11 of 20
CS103-Computer Programming
Page 12 of 20
CS103-Computer Programming
Page 13 of 20
CS103-Computer Programming
Page 14 of 20
#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
Page 15 of 20
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
Page 16 of 20
CS103-Computer Programming
Page 17 of 20
CS103-Computer Programming
Page 18 of 20
CS103-Computer Programming
Page 19 of 20
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;
CS103-Computer Programming
Page 20 of 20
CS103-Computer Programming