0% found this document useful (0 votes)
35 views

2-Classes and Objects-1

Object-oriented programming (OOP) is a programming paradigm based on the concept of classes and objects. OOP uses classes to structure a programming language into reusable pieces of code called classes, which can then be used to create instances of objects. The key characteristics of OOP include classes, objects, data abstraction, encapsulation, inheritance, and polymorphism.

Uploaded by

Dishant Balotra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

2-Classes and Objects-1

Object-oriented programming (OOP) is a programming paradigm based on the concept of classes and objects. OOP uses classes to structure a programming language into reusable pieces of code called classes, which can then be used to create instances of objects. The key characteristics of OOP include classes, objects, data abstraction, encapsulation, inheritance, and polymorphism.

Uploaded by

Dishant Balotra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

What is OOP ?

• Object-Oriented Programming (OOP) System is a


programming paradigm based on the concept of using
classes and objects in your programming code.

• Object-Oriented Programming (OOP) used to structure a


programming language into simple, reusable pieces of
code that usually known as classes, which we can use as
a user define Data Type to create instances of objects.

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.

So here, Car is the class and wheels, speed limits, mileage


are their properties.

3
Object
• An Object is an identifiable entity with some
characteristics and behaviour.

• An Object is an instance of a Class.

• When a class is defined, no memory is allocated but when


it is instantiated (i.e. an object is created) memory is
allocated

4
Object
• An Object is an identifiable entity with some
characteristics and behaviour.

• An Object is an instance of a Class.

• When a class is defined, no memory is allocated but when


it is instantiated (i.e. an object is created) memory is
allocated

• When a program is executed the objects interact by


sending messages to one another.

• Each object contains data and code to manipulate the


data.

5
Data Abstraction
• Data abstraction is one of the most essential and
important features of object-oriented programming in C++.

• Abstraction means displaying only essential information


and hiding the details.

• Data abstraction refers to providing only essential


information about the data to the outside world, hiding the
background details or implementation.

Consider a real-life example of a man driving a car. The man


only knows that pressing the accelerators will increase the
speed of the car or applying brakes will stop the car but he
does not know about how on pressing accelerator the speed
is actually increasing, he does not know about the inner
mechanism of the car or the implementation of accelerator,
brakes etc in the car. This is what abstraction is.
6
Data Encapsulation
• In normal terms, Encapsulation is defined as wrapping up
of data and information under a single unit.

• In Object-Oriented Programming, Encapsulation is defined


as binding together the data and the functions that
manipulate them.

Consider a real-life example of encapsulation, in a company,


there are different sections like the accounts section, finance
section, sales section etc. Now there may arise a situation
when for some reason an official from the finance section
needs all the data about sales in a particular month. In this
case, he is not allowed to directly access the data of the
sales section. He will first have to contact some other officer
in the sales section and then request him to give the
particular data. This is what encapsulation is.
7
Inheritance
• The capability of a class to derive properties and
characteristics from another class is called Inheritance.

• Inheritance is one of the most important features of


Object-Oriented Programming.

• Inheritance supports the concept of “reusability”, i.e. when


we want to create a new class and there is already a class
that includes some of the code that we want, we can
derive our new class from the existing class. By doing this,
we are reusing the fields and methods of the existing
class.

Example: Dog, Cat, Cow can be Derived Class of Animal


Base Class.

8
Polymorphism
• The word polymorphism means having many forms.

• In simple words, we can define polymorphism as the


ability of a message to be displayed in more than one
form.

Example : A person at the same time can have different


characteristic. Like a man at the same time is a father, a
husband, an employee. So the same person posses different
behaviour in different situations. This is called polymorphism.

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

Header class Rectangle


class class_name {
{ private:
permission_label:
int width;
member;
permission_label:
int length;
Body
member; public:
... void set(int w, int l);
}; int area();
};
12
Class Definition - Access Control
• Information hiding
– To prevent the internal representation from direct
access from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends
of this class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program

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

• To define a member function outside the class definition we


have to use the scope resolution :: operator along with class
name and function name

18
Define a Member Function

class name

member function name

void Rectangle :: set (int w, int l)


{
width = w;
length = l;
scope operator
}
19
Class Definition
Member Functions
• const member function
– declaration
• return_type func_name (para_list) const;
– definition
• return_type func_name (para_list) const { … }
• return_type class_name :: func_name (para_list) const { … }
– Makes no modification about the data members (safe
function)
– It is illegal for a const member function to modify a
class data member

20
Const Member Function
class Time
{
private : function declaration
int hrs, mins, secs ;

public :
void Write ( ) function definition
const ;
};

void Time :: Write( ) const


{
cout <<hrs << “:” << mins << “:” << secs << endl;
}

21
What is an object?

OBJECT
set of methods
Operations (member functions)

Data internal state


(values of private data members)

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;

void set(int w, int l); r2.set(8,10);


int area(); cout<<r2.area()<<endl;
}
};

24
Another Example
#include <iostream.h> // member function definitions

class circle void circle::store(double r)


{ {
radius = r;
private: }
double radius;
double circle::area(void)
public: {
return 3.14*radius*radius;
void store(double); }
double area(void);
void display(void); void circle::display(void)
{
cout << “r = “ << radius << endl;
}; }
int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display(); 25
}
Declaration of an Object
r1 is statically allocated
class Rectangle
{ main()
private: {
Rectangle r1;
int width;
r1.set(5, 8);
int length;
}
public:
void set(int w, int l);
r1
int area(); width = 5
length = 8
};

26

You might also like