Chapter 3
Chapter 3
LECTURE 3
INTRODUCTION TO CLASSES
AND OBJECTS IN C++
Objective
MULTIMEDIA UNIVERSITY 1
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
3.0 Class
A class is a blueprint or prototype that defines the variables (data) and methods (code)
common to all objects of a certain kind.
The class serves as a user defined type.
A class thus defines a data type that behaves like the built-in types of a programming
language.
An object is an individual instance of a class.
Access to the variables and methods declared in a class depend on whether they are
declared private or public.
A variable declared as public is visible everywhere.
A variable declared private is not visible outside the class.
Classes contains a few objects.
class ClassName
{
// Data definition
// Method
definition
};
class Rectangle
{ int length, width;
public :
double calculate_area()
{ return length*width}
};
MULTIMEDIA UNIVERSITY 2
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
3.1 Methods
class Student
{
int ID,age;
char name[30];
double mark;
public:
void set_data (int a,int b, char * c, double d)
{
ID=a;
age=b;
strcpy(name,c);
mark=d;
}
};
2. Example below shows how to define a method outside the class using the
scope resolution operator ( :: ).
class Student
{
int ID,age;
char name[30];
double mark;
public:
void set_data (int a,int b,char * c,double d);
};
Value that
will be return void Student::set_data(int a,int b,char * c,double d)
{
ID=a;
age=b;
strcpy(name,c);
mark=d;
}
Class name
Parameter list
Function/Method name
MULTIMEDIA UNIVERSITY 3
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Data members and methods can be declared using access control to specifically specify
access to members of the class.
There are three types of access control:
1. Public
2. Private
3. Protected
Class members are private members by default but the situations can be changed by
using another keyword. For example, using public and protected keyword.
1. Public
Public members are accessible from outside and also inside of the class
(for example in main() function).
Members in the class must be declared in the public section to make it
accessible from outside the class.
Definition of friend does not give any effect to this access control.
Example of the use of this keyword in the real world: student name,
semester.
#include<iostream.h>
class SQUARE
{
public:
int length; //data member
void change(); //method
}S1;
void main()
{
cout<<"length : "<<S1.length<<endl;
S1.length = 100;
cout<<"length : "<<S1.length<<endl;
S1.change();
cout<<"length : "<<S1.length<<endl;
}
MULTIMEDIA UNIVERSITY 4
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
2. Private
Private members can only be retrieved by functional members and friends for
a class, which has been declared.
Declaration of friend will not cause any effect to the access control.
Example of the use of this keyword in the real world:
Usernames and passwords to open an e-mail or banking accounts is declared
privately so that it is not accessed by any other unauthorized person.
#include<iostream.h>
class SQUARE
{
private:
int length;//data member
public:
void display();
}S1;
void main()
{
cout<<"length : "<<S1.length<<endl;
S1.display();
}
MULTIMEDIA UNIVERSITY 5
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
3. Protected
Protected members are accessible in the class where it is declared and also
any other classes, which are inherited from that class.
Definition of friend does not give any effect to this access control.
Will be explained in later chapters
MULTIMEDIA UNIVERSITY 6
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Constructor
o Create data storage for an object in memory space.
o A constructor is automatically called whenever an instance/object of a class is
created.
o Must be the same name as the class name.
o A constructor has no return type.
Destructor
o Release any computer resources, which has been specified to an object.
o A destructor has no return type.
class Employee
{
public :
I/O
o Enable data to be received as input or displayed as output.
Copying or cloning
o Enable an object to be copied but both of the objects must be of the same type.
Both object R1 and R are objects which are of the Rectangle R,R1;
same type. Therefore object copying can be done.
R1=R;
Iterator
o At is used for a class, which has a collection of data item. By using iterator, each
data can be accessed individually.
class sequentialList
{ private :
Iterator int item[MAX] ,num_item;
public: ……………};
MULTIMEDIA UNIVERSITY 7
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Comparison operator
o Compare between one object with another, which are of the same type.
class AB
{ :
:
};
main()
{
AB ob1,ob2;
if(ob1==ob2)
cout <<”ob1 is equal to ob2”;
else
cout <<”ob1 is not equal to ob2”;
}
Accessor
o Can access or compute values from object without doing any modification to the
object.
Mutator
o Modify the object using certain ways (may be by changing one value or more value)
MULTIMEDIA UNIVERSITY 8
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
# include <iostream.h>
# include <string.h>
class student
{
// If the access method is not specified,
// by default it will be set to private
char id[20];
char name[30],status[15];
double gpa;
public:
char* student::get_status()
{
if(gpa>=2.0)
strcpy(status,"PASS"); This method will manipulate the data
else
strcpy(status,"FAIL");
(gpa) but cannot change the value of
data field
return status;
}
void student::print_data()
{
cout<<"Student's id : "<<id<<endl;
cout<<"Student's name : "<<name<<endl;
cout<<"Student's gpa : "<<gpa<<endl;
}
main()
{
student s; // create an object called s
char name[30],id[10];
double gp;
Output
cout<<"Enter your name:";
cin.getline(name,30);
cout<<"Enter your ID:"; Enter your name:Albert
cin.getline(id,10); enter your ID:1234
cout<<"Enter Your GPA:"; Enter Your GPA:3.33
cin>>gp;
cout<<'\n'; Student's id : 1234
s.set_data(id,name,gp);
Student's name : Albert
s.print_data();
cout<<"Status : "<<s.get_status(); Student's gpa : 3.33
cout<<'\n'; Status : PASS
return 0;
} Press any key to continue
MULTIMEDIA UNIVERSITY 9
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
3.4 Objects
Declaring an object
Example:
Assuming that you have created an object from the class Staff. Example of declaration is
shown as below:
Staff S1;
When an object has been declared, it can be manipulated by any function, which is
declared inside the object class.
Syntax is used to call a data or method: ObjectName . MemberName
Dot operator
Object of type Staff is accessible by the members from Staff class only.
MULTIMEDIA UNIVERSITY 10
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
#include<iostream.h>
#include<string.h>
class Staff
{
private :
char staff_name[30];
int staff_id;
public :
void get_input()
{
strcpy(staff_name, "David Johnson");
staff_id = 1234;
}
void display()
{
cout<<"Staff Name : "<<staff_name<<endl;
cout<<"Staff ID : "<<staff_id<<endl;
}
}S1, S2; Object declaration in class
void main()
{
Staff S3; Object declaration in main()
S3.get_input();
S3.display();
}
MULTIMEDIA UNIVERSITY 11
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Example below shows how an object can access members from a class.
#include<iostream.h>
#include<math.h>
class Square
{
//by default length is private
int length;
public:
void set_data(int x)
{
length=x;
}
double area()
{
return pow(length,2);
}
};
void main ()
{
// object declaration
Square S1, S2;
cout<<"Area of S2 is:"<<S2.area()<<endl;
Object list
You can declare as many objects as you want from the same class.
The example shows how objects are declared from the a class.
Class Box
{
private:
int length, width;
public:
void set_data();
}B1, B2;
void main()
{
Box B3, B4;
}
MULTIMEDIA UNIVERSITY 12
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Array of objects
The syntax for declaring an array of objects is the same as declaring an array of any
variables.
The way of accessing array of object is also the same as accessing an array of any
variables.
Example shown below is a program, which has an object samp with four array elements.
#include<iostream.h>
class Box
{ Output
//by default length is private
int length, width;
Area of B[0] is:0
public: Area of B[1] is:2
Area of B[2] is:6
void set_data(int x, int y) Area of B[3] is:12
{ Area of B[4] is:20
length=x;
width = y; Press any key to continue
}
double area()
{
return length * width;
}
}B1[5];
void main ()
{
This program has an object B1, which has 5 array elements where each element has the
value between 0 to 4.
MULTIMEDIA UNIVERSITY 13
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
Assigning object
An object can be assigned to another object given that both of the objects must be of
the same type.
Automatically, when an object is assigned to another object, all data members will be
copied.
Example below shows how an object is assigned to another object.
#include<iostream.h>
void main ()
{
Box B2;
B1.set_data(3,4);
B2.set_data(5,2);
B2 = B1;
MULTIMEDIA UNIVERSITY 14
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
# include <iostream.h>
class OBJECT
{ int length;
public:
void set_length (int x)
{
length=x;
}
void print()
{
cout<<length<<endl;
}
};
void main()
{
OBJECT OB;
OB.set_length(10);
PRINTOBJ(OB);
OB.print(); // the output remain 10, value of length has not
// been changed
}
Output
10
100
10
Press any key to continue
Pointer to object
You can access an object either by direct access or using pointer to object. Recall: A
pointer to an object is a variable that contains the memory address of another object.
In order to access the specified element in an object
o dot operator (.) can be used, with indirection operator(*)
o arrow operator (->) instead of the dot operator. This -> is called the indirect
member access operator.
The syntax to declare a pointer to an object is shown as below:
MULTIMEDIA UNIVERSITY 15
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
#include<iostream>
using namespace std; * Name of pointer to an object
class Point{
private: double x, y, z;
public:
x=x_arg;
y=y_arg;
z=z_arg;
}
double X( ){return x;}
double Y( ){return y;}
double Z( ){return z;}
};
void main( ){
Point p;
p.set_val(1.0,2.0,3.0);
MULTIMEDIA UNIVERSITY 16
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
MULTIMEDIA UNIVERSITY 17
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
class TheClass
{
private:
int a,b;
public:
void SetValues(int aa, int bb)
{
a=aa;
b=bb;
PrintValues();
void PrintValues(){
cout<<"a : "<<a<<", b : "<<b<<endl;
};
Prototypes includes pointers
void Func2(TheClass*);
Note: The first line is through pointer, the second line is the output through the object. Notice
that when a pointer is pointing to an object (when you assign the address of an object to a
pointer), the pointer can modify the properties of the object by invoking the member functions
defined in the same class of the object.
#include<iostream>
using namespace std;
class TheClass
{
private:
int a,b;
public:
MULTIMEDIA UNIVERSITY 18
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
void PrintValues(){
cout<<"a : "<<a<<", b : "<<b<<endl;
};
void main()
{
TheClass obj;
Func2(obj); Pass the address of obj implicitly to &ref
obj.PrintValues();
Output:
a : 2, b : 3
a : 2, b : 3
Press any key to continue
Note: See that output is the same as the example(pass by pointer) example above. The
difference in the program is just the function prototype, call and actual function. There are no
changes to the class
MULTIMEDIA UNIVERSITY 19
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
#include<iostream.h>
class SQUARE
{
private:
int length;
Output
public:
inline void set_length();
inline void display(); Length : 10
}SQ1; Press any key to continue
void SQUARE :: set_length()
{
length = 10;
}
void main()
{
SQ1.set_length();
SQ1.display();
}
MULTIMEDIA UNIVERSITY 20
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
#include<iostream.h>
class SQUARE
{
private:
int length;
public:
void set_length() // automatically inline
{
length = 10;
}
void display()// automatically inline
{
cout<<"Length : " <<length<<endl;
}
}SQ1;
void main()
{
SQ1.set_length();
SQ1.display();
}
MULTIMEDIA UNIVERSITY 21
DCS 5088 OBJECT ORIENTED PROGRAMMING LECTURE 3
# include <iostream.h>
# include <string.h>
~club(void)
{cout<<"\nThank You";} Destructor
void set_data(char [], char [], int);
void print_data();
char* get_status();
};
char* club::get_status()
{ Accessor
if(point >= 1000)
strcpy(status,"VIP");
else Comparison
strcpy(status,"Executive");
return status;
}
void club::print_data()
{
cout<<"ID: "<<id<<endl;
cout<<"Name: "<<name<<endl; I/O
cout<<"Point: "<<point<<endl;
}
int main()
{ Object declaration
club s, s1;
char name[30], id[10];
int pt;
cout<<s.get_status()<<endl;
cout<<'\n';
s1=s;
Copying and cloning
s1.print_data();
cout<<s1.get_status()<<endl;
return 0;
}
MULTIMEDIA UNIVERSITY 22