Chapter 08
Chapter 08
Chapter 08
Outline
• C++ Class and Objects
• Inheritance in c++
• Polymorphism in c++
• Encapsulation in c++
• Function Overloading in c++
• Exception Handling in c++
Object oriented programming
• programming paradigm that uses classes and
objects to structure code and data.
• object-oriented programming is about creating
objects that contain both data and functions.
Procedural programming vs oop
• Procedural programming and object-oriented
programming (OOP) are two different programming
paradigms.
• Procedural programming is a programming paradigm
that structures code around procedures, functions, or
subroutines.
• Procedural programming focuses on the sequence of
steps taken to solve a problem and emphasizes the
use of functions to modularize code.
Cont..
• OOP is a programming paradigm that structures
code around objects and classes.
• OOP is a programming paradigm focuses on the
data and behavior of objects and emphasizes the
use of inheritance, polymorphism, and
encapsulation.
Advantages of OOP
• Encapsulation: OOP allows you to encapsulate data
and behavior into objects, which helps to protect your
code and prevent unintended changes to the data.
• Abstraction: OOP allows you to abstract away the
implementation details of a class and focus on its
interface, which makes it easier to use and
understand.
Cont..
• Inheritance: OOP allows you to create new classes
that inherit properties and methods from existing
classes, which can save time and reduce code
duplication.
• Polymorphism: OOP allows you to write code that
can work with objects of different types, which
makes your code more flexible and reusable.
• Modularity: OOP allows you to modularize your code
into smaller, more manageable pieces, which makes
it easier to maintain and update.
Class and object
• a class is a blueprint or template for creating objects.
• It defines the properties and methods that an object
will have.
• class is a user-defined data type that encapsulates data
and functions that operate on that data.
• A class definition typically includes member variables,
which are the data components of the class, and
member functions, which are the functions that
operate on that data.
• The member variables and functions are encapsulated
within the class,
Cont..
• object is an instance of a class.
• It is created from the blueprint or template
defined by the class.
• it has its own set of values for the member
variables defined in the class.
Object
Class Apple
Fruit Banana
Mango
Create a class
Step 1: Define the class: Use the "class" keyword followed by
the name of the class to define the class.
Syntax:
class className {
Access modifiers: // member variables and functions go here };
Example:
class people{
public:
// member variables and functions go here
};
Cont..
Step 2: Define member variables: Inside the class
definition, define any member variables that the class will
have.
• Member variables are the data components of the
class and are accessed using the dot notation. For
example:
class people {
public:
string name;
int age;
};
cont..
Step 3: Define member functions: Inside the class definition, define any
member functions that the class will have.
• Member functions are the functions that operate on the member
variables of the class and are accessed using the dot notation.
For example:
class people {
public:
string name;
int age;
void printInfo() {
cout << "My name is" << name << " and age is" << age << endl;
}
};
object.
Step 5: Create objects: Once you have defined a
class, you can create objects of that class using
the following syntax:
className objectName;
Example:
people person1;
This creates an object called “person1" of the
“people" class.
Access member variables and functions