Object Oriented Programming:
C++
Objects and Classes
Lecture Outline
• Categories of Programming Languages
1) Traditional/Procedural
2) Object Oriented
• Objects and classes
• Class definition
• Access levels: Private, public
• Constructor
• Destructor
• Default copy constructor
• Structures and Classes
• Static class data
Broad Categories of Programming
Languages
• Programming languages may be divided into two
(02) broad categories:
– Traditional/Procedural programming languages
– Object-oriented programming languages
Why Do We Need OOP?
Object-oriented programming was developed because
limitations were discovered in earlier approaches to
programming.
Why Do We Need OOP?
• There are two related problems with procedural
languages:
1. Functions have unrestricted access to global data
2. Unrelated functions and data
Why Do We Need OOP?
• Procedural languages: Unrestricted access
– In large programs, there are many functions and many global data.
This large number of connections causes problems in several ways:
1. Program structure is difficult to conceptualize
2. Program is difficult to modify e.g., changing global data from short to long
Why Do We Need OOP?
• Procedural languages: Unrelated functions and data
– The second – and more important – problem with the
procedural paradigm is that its arrangement of separate data
and functions does a poor job of modelling things (objects) in
the real world e.g., people and cars.
– Real world objects have both attributes (data) and behaviour
(functions)
• The Object-oriented approach:
– The fundamental idea behind object-oriented language is to
combine into a single unit both data and functions that operate
on that data.
• Such a unit is called an Object.
Characteristics of OO Languages: Objects
• In OOP program is divided into object rather
than functions.
• Object can be anything:
– Physical objects
• Automobiles in a traffic-flow simulation
• Countries in an economics model
– Human entities
• Employees
• Customers
• Departments
– Components in computer games
• Cars in auto race
Characteristics of OO Languages:
Classes
• A class serves as a plan, template or specification for
a number of similar objects.
• A class specifies what data and what functions will be
included in objects of that class
• Examples: Vehicle, Animal, and Students classes
Characteristics of OO Languages:
Classes
OOP Approach
OOP Approach
• An object’s functions (called member functions in C++)
typically provide the only way to access its data
• Object’s data and member functions are said to be
encapsulated. They can’t be accessed directly without
the permission of the object and are thus safe from
accidental modifications
• Example:
– Objects as departments – such as I.T, finance, examination, and
so on - in a University
OOP Approach: University Paradigm
BCS Dept.
Dept. Data
Head of
Dept.
Clerks
Finance Dept. Examine Dept.
Financial Examine
Data Data
Director Controller Of
Finance Examination
Financial
Clerks
Assistant
Characteristics of OO Languages
• Objects
• Classes
• Inheritance
• Reusability
• Polymorphism and Overloading
First Object Oriented Program
#include<conio.h>
Keyword
Name of Class
Keyword Private and colon
Keyword Public and colon getch();
Braces
Semicolon
Private and Public Data/Functions
• Within the body, the keywords private: and public: specify
the access level of the members of the class.
• the default is private.
Not accessible from
outside class
Accessible from
outside class
Private and Public Data/Functions
• This class example shows how we can
encapsulate (gather) a circle information into
one package (unit or class)
No need for others classes to
class Circle access and retrieve its value
directly. The class methods are
{
responsible for that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
Class Exercise
• Write a program using objects/classes that
calculates the area of a rectangle and displays it
on the screen. HINT: area = length * width.
Class Exercise: Solution
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
class rect_area
rect_area a;
{
private:
a.get_area(5, 3);
int area;
a.show_area();
public:
void get_area(int l, int w)
getch();
{ area = l * w; }
}
void show_area()
{ cout << “Area of rect. Is ” << area; }
};
Class Exercise
• Develop a database for student that operates as
shown in following figure:
Constructor
• Is it possible to initialize member-data in class
definition? Example, is it possible to say:
Data members can only be
class someobj initialized using constructors:
{
LE class someobj
private: IB {
S S private:
P O somedata = 5;
int
int somedata ;
OT
Npublic: public:
someobj()
};
{ somedata=5; }
};
Constructor
• Constructor is special member function that is
called automatically when an object is created.
• Constructor is used to initialize member data
• Constructor has the same name as the class and
has no return type.
• Constructors are always public
Constructor: Counter Example
#include<conio.h>
getch();
Destructor
• Destructor is special member function that is
called automatically when an object is destroyed.
– The most common use of destructor is to deallocate
memory that was allocated for the object by the
constructor
• Like constructor, destructor has the same name
as the class and has no return type
• Unlike constructor, destructor takes no
arguments and is preceded by a tilda (~)
Destructor
Constructor-Destructor: Example
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
class Foo
Foo a;
{
private:
a.Foo();
int data;
a.~Foo();
public:
Foo() : data(0)
getch();
{ cout<<"object created\n"; }
}
~Foo()
{ cout<<"object destroyed\n"; }
};
Assignment
1) Write two programs implementing ideas on classes
and objects.
2) Write a program that makes use of constructor
and destructor. The program must display the
result of the operations of constructor and
destructor.
Note: You must explain your programs
Objects and Memory
class smallobj
• Object is a collection of
both data and functions somedata Specification for
smallobj objects
• If a class has more than
one object, all the
objects have their own
data, however, they all Two instances
operate on the same of somedata
smallobj s1, s2; are created in
functions i.e., member
functions are created memory for
objects s1, and
and placed in memory s2. However,
s1 s2
only once - when they member
are defined in the class somedata somedata functions are
definition. common to
1066 1776 both objects
Objects and Memory
Static Class Data
• Each object contains its own data, however, if a
data member is declared as static, only one such
item is created for the entire class, no matter how
many objects there are.
• A static data item is useful when all objects of the
same class must share a common item of
information
• Application example is a road-racing game, where a race car
might want to know how many other cars are still in the
race.
Static Class Data
Static Class Data
#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
class foo
foo f1, f2, f3;
{
private:
cout << “count is “ << f1.getcount() << endl;
static int count;
cout << “count is “ << f2.getcount() << endl;
public:
foo()
cout << “count is “ << f3.getcount() << endl;
{ count++; }
getch();
int getcount()
}
{ return count; }
};
int foo::count = 0;
Home Work
• Objects as function arguments
• Overloaded constructors
• The default copy constructor
• Returning objects from functions
• Structures vs Classes