Class
Class
C++
• 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
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
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.
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.
#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.
int foo::count = 0;
Home Work
• Objects as function arguments
• Overloaded constructors
• The default copy constructor
• Returning objects from functions
• Structures vs Classes