Classes in Data Structure AND Object Oriented Programing
Classes in Data Structure AND Object Oriented Programing
Classes in Data Structure AND Object Oriented Programing
Oriented Programming
Classes
Dr Ayesha Zeb
Email: [email protected]
Lecture Contents
• Introduction
• Class definition
• Classes vs structures
• Accessing data members
• Member functions
• Constructors
• Destructors
• Class activity
• Overloading constructor
• Pointer to a class
Introduction
• Like structures, classes are also user defined types that
specify how objects of its type can be created and used
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Defining a C++ class (contd.)
• Where class_name is a valid identifier for the class,
object_names is an optional list of names for objects of
this class. The body of the declaration can contain
members, which can either be data or function
declarations, and optionally access specifiers.
• An access specifier is one of the following three
keywords: private, public or protected. These
specifiers modify the access rights for the members that
follow them:
• private members of a class are accessible only from within
other members of the same class
• protected members are accessible from other members of the
same class, but also from members of their derived classes.
• Finally, public members are accessible from anywhere where
the object is visible.
Defining a C++ class (contd.)
• By default, all members of a class declared with the
class keyword have private access for all its members.
Therefore, any member that is declared before any other
access specifier has private access automatically.
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void); };
• The above definition is the same as follows:
class Rectangle {
private:
int width, height;
public:
void set_values (int,int);
int area (void);};
Defining C++ objects
• A class provides the blueprints for objects, so basically
an object is created from a class. We declare objects of
a class with exactly the same sort of declaration that we
declare variables of basic types. For example, the
following statements declare two objects of class
Rectangle:
Rectangle rect1;
Rectangle rect2;
• Both of the objects rect1 and rect2 will have their own
copy of data members.
Classes vs. Structures
• Class members are private by default, whereas a
struct is a class where members are public by default:
struct X {
int m;
// … };
• Means
class X {
public:
int m;
// … };
Classes vs. Structures
• So, unlike structures, we can use access specifiers with
classes, meaning a structure is not safe in the sense it
could not hide its implementation details from the end-
user, whereas a class is secure as it may hide its
programming and design details.
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; //Store the volume of box here
Accessing data members
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0; }
Class member functions
• A member function is a function that has its definition or
its prototype within the class definition like any other
variable. It operates on any object of the class of which it
is a member, and has access to all the members of a
class for that object.
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};
Class member functions (contd.)
• Member functions can be defined within class definition
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void) {
return length * breadth * height;
}
};
• Or separately using scope operator (:: two colons)
double Box::getVolume(void) {
return length * breadth * height;
}
Class member functions (contd.)
• Let us put above concepts to set and get the value of
different class members in a class
#include <iostream>
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
The Class Constructor
• A class constructor is a special member function of a
class that is executed whenever we create new objects
of that class.
private:
double length;
};
The Class Constructor (contd.)
// Member functions definitions including
constructor
Line::Line( double len) {
cout << "Object is being created, length = " <<
len << endl;
length = len;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
The Class Constructor (contd.)
return 0;
}
The Class Destructor
• A destructor is a special member function of a class
that is executed whenever an object of it's class goes out
of scope or whenever the delete expression is applied to
a pointer to the object of that class.
class Box {
public:
Box() {
cout<<“Box-type object is created” << endl;
}
~Box() {
cout<<“Box-type object is deleted” << endl;
}
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void);// Returns box volume
};
Class activity
• Write a C++ program in which you declare a class with
defined members (all public) for the following information
• Student name
• Student degree no.
• Student Section
• Students CGPA
• Constructor
• A member function that prints all of the above data
• Destructor
class Rectangle {
int width, height;
public:
Rectangle ();
Rectangle (int,int);
int area (void) {return
(width*height);}
};
Overloading Constructor (contd.)
Rectangle::Rectangle () {
width = 5;
height = 5;
}
Rectangle::Rectangle (int a, int b) {
width = a;
height = b;
}
int main () {
Rectangle rect (3,4);
Rectangle rectb;
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
Pointer to a Class
• A pointer to a C++ class is done exactly the same way
as a pointer to a structure and to access members of a
pointer to a class we use the member access operator
(->), just as you do with pointers to structures. Also as
with all pointers, you must initialize the pointer before
using it.
Box Box1;
Box *class_pointer;
class_pointer = &Box1;
Pointer to a Class (contd.)
• Example:
#include <iostream>
using namespace std;
class Box {
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h; }
double Volume() {
return length * breadth * height; }
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
Pointer to a Class (contd.)
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
return 0;
}
Acknowledgements/References
• https://www.tutorialspoint.com/cplusplus/index.htm
• https://www.cplusplus.com/