2-Classes and Objects-1
2-Classes and Objects-1
1
Characteristics of OOP ?
OOPs ( Object-oriented programming system ) has many
Characteristics like:
• Class
• Objects
• Data Abstraction
• Data Encapsulation
• Inheritance
• Polymorphism
2
Class
• The building block of C++ that leads to Object-Oriented
programming is a Class.
For Example:
Consider the Class of Cars.
There may be many cars with different names and brand but
all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc.
3
Object
• An Object is an identifiable entity with some
characteristics and behaviour.
4
Object
• An Object is an identifiable entity with some
characteristics and behaviour.
5
Data Abstraction
• Data abstraction is one of the most essential and
important features of object-oriented programming in C++.
8
Polymorphism
• The word polymorphism means having many forms.
9
Object-Oriented Programming
Introduction to Classes
• Class Definition
• Class Examples
• Objects
• Constructors
• Destructors
10
Class
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and inheritance
• Class
– A user defined type
– Consists of both data and methods known as data members and
member functions
– Data members are the data variables and member functions are
the functions used to manipulate these variables
– Defines properties and behavior of that type
• Data Abstraction
– Separate the implementation details from its essential properties
11
Define a Class Type
13
Class Definition
Access Control
• The default access specifier is private
• The data members are usually private or protected
• A private member function is a helper, may only be
accessed by another member function of the same
class (exception friend function)
• The public member functions are part of the class
interface
• Each access control section is optional,
repeatable, and sections may occur in any order
14
Class Definition
Member Functions
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
15
Member function in classes
• There are 2 ways to define a member function :
1. Inside class definition
2. Outside class definition
Inside class definition
• All the member functions defined inside the class
definition are by default inline, but you can also make
any non-class function inline by using keyword inline with
them.
• Inline functions are actual functions, which are copied
everywhere during compilation, like pre-processor
macro, so the overhead of function calling is reduced.
16
Define a Member Function Inside a
class
class Rectangle
{
private:
int width, length;
public:
void set (int w, int l);
int area() {return width*length; }
};
inline
17
Member function in classes
Outside class definition
18
Define a Member Function
class name
20
Const Member Function
class Time
{
private : function declaration
int hrs, mins, secs ;
public :
void Write ( ) function definition
const ;
};
21
What is an object?
OBJECT
set of methods
Operations (member functions)
22
Classes & Objects
Objects: Instance of a class
class Rectangle
{
Rectangle r1;
private: Rectangle r2;
int width; Rectangle r3;
int length;
……
public:
void set(int w, int l);
int area();
int a;
};
23
Declaration of an Object
class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;
24
Another Example
#include <iostream.h> // member function definitions
26