16.1.
1 Explain Class and Object
• OOP stands for Object Oriented Programming
• Procedural programming use procedure and functions that perform actions
on data.
• object-oriented programming is about creating objects that contain both data
and functions.
16.1.1 Explain Class and Object
• Object-oriented programming has several advantages over procedural
programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and
shorter development time
16.1.1 Explain Class and Object
16.1.1 Explain Class and Object
• So, a class is a template for objects, and an object is an instance of a class.
• C++ is an object-oriented programming language.
• Everything in C++ is associated with classes and objects, along with its attributes
and methods. For example: in real life, a car is an object. The car has attributes, such
as weight and color, and methods, such as drive and brake.
• Attributes and methods are basically variables and functions that belongs to the
class. These are often referred to as "class members".
• A class is a user-defined data type that we can use in our program, and it works as
an object constructor, or a "blueprint" for creating objects.
16.1.1 Explain Class and Object
16.1.2 Write A C++ Program To Declare A
Class Along With Data Members And
Member Functions
16.1.2 Write A C++ Program To Declare A
Class Along With Data Members And
Member Functions
16.2.1 Differentiate Between Private And
Public Access Specifiers
• Private keyword in the previous example shown is the access specifier
• Access specifiers define how the members (attributes and methods) of a class
can be accessed.
• C++ has THREE specifiers
• Public: Member are accessible from outside class
• Private: members cannot be accessed or viewed from outside the class
• Protected: Members cannot be accessed from outside the class however, they
can be accessed in inherited classes.
16.2.2 Write a C++ Program In Which Class
Members Are Not Accessible Outside The Class
16.2.2 Write a C++ Program In Which Class
Members Are Not Accessible Outside The Class
16.2.3 Write A C++ Program In Which Class
Members Are Accessible From Anywhere
16.2.4 Write A C++ Program In Which A Class
Uses Both Public And Private Access Specifiers
16.2.4 Write A C++ Program In Which A Class
Uses Both Public And Private Access Specifiers
16.3.1 Explain The Concept Of Encapsulation
• Encapsulation is one of the key features of object-
oriented programming. It involves the bundling of
data members and functions inside a single class.
• Bundling similar data members and functions inside
a class together also helps in data hiding.
16.3.1 Explain The Concept Of Encapsulation
• In general, encapsulation is a process of wrapping similar code in one place.
• In C++, we can bundle data members and functions that operate together
inside a single class. For example,
• Here, the variables and functions can be accessed from other classes as well.
Hence, this is not data hiding.
•
16.3.1 Explain The Concept Of Encapsulation
16.3.2 Differentiate Between Constructor And
Destructor
• A constructor is a special type of member function that is called
automatically when an object is created.
• In C++, a constructor has the same name as that of the class and it does not
have a return type. For example,
• Here is a wall is a constructor of class wall.
• has the same name as the class,
• does not have a return type, and
• Is public
16.3.2 Differentiate Between Constructor And
Destructor
• C++ destructor is a special member function that is executed automatically
when an object is destroyed that has been created by the constructor. C++
destructors are used to de-allocate the memory that has been allocated for
the object by the constructor.
16.3.3 Differentiate Among The Types Of
Constructor
• Properties of a constructor:
• Constructor name is same as class name.
• They should be declared or defined in public section of class.
• They don’t have any return type like void, int, etc and therefore can’t return
values.
• They can’t be inherited.
• They are the first member function of a class to be executed.
16.3.2 Differentiate Between Constructor And
Destructor
16.3.2 Differentiate Between Constructor And
Destructor
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
• User Defined Constructor:
• The constructor that accepts parameter to initialize the data members of an
object are called user defined constructor. Programmer has to explicitly
define such constructor in the program
• Copy Constructor:
• Constructor having object reference as parameter is called copy constructor.
A copy constructor when called, creates an object as an exact copy of
another object in terms of its attribute. Copy constructor can be called by
passing object as parameter or using assignment operator.
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
Default Constructor:
• A constructor with no parameters is known as a default constructor. In the
example below:
16.3.3 Differentiate Among The Types Of
Constructor
• Constructor Overloading:
• Constructor overloading means having more than one constructor with the
same name. Constructors are methods invoked when an object is created.
• You have to use the same name for all the constructors which is the class
name. This is done by declaration the constructor with a different number of
arguments.
16.3.3 Differentiate Among The Types Of
Constructor
16.3.3 Differentiate Among The Types Of
Constructor
16.3.4 Describe Inheritance In Object
Oriented Programming
• Inheritance is one of the key features of Object-oriented programming in
C++. It allows us to create a new class (derived class) from an existing class
(base class).
• The derived class inherits the features from the base class and can have
additional features of its own.
• In C++, it is possible to inherit attributes and methods from one class to
another. We group the "inheritance concept" into two categories:
• derived class (child) - the class that inherits from another class
• base class (parent) - the class being inherited from
16.3.4 Describe Inheritance In Object
Oriented Programming
16.3.5 Write The Basic Syntax Of Inheritance
Using Base Class And Derived Class
16.3.5 Write The Basic Syntax Of Inheritance
Using Base Class And Derived Class
16.3.5 Write The Basic Syntax Of Inheritance
Using Base Class And Derived Class
16.3.5 Write The Basic Syntax Of Inheritance
Using Base Class And Derived Class
16.3.6 List The Names Of Three Inheritance
Access Specifiers
In C++ inheritance, we can derive a child class from the base class in different
access modes. For example, public mode and
alternatively, we can also derive classes in protected or private modes.
16.3.6 List The Names Of Three Inheritance
Access Specifiers
In C++ inheritance, we can derive a child class from the base class in
different access modes. For example,
16.3.6 List The Names Of Three Inheritance
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For
Three Access Specifiers
16.3.7 Write The Syntax Of Inheritance For
Three Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.7 Write The Syntax Of Inheritance For Three
Access Specifiers
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• Inheritance is one of the key features of Object-oriented programming in
C++. It allows us to create a new class (derived class) from an existing class
(base class).
• The derived class inherits the features from the base class and can have
additional features of its own. For example
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• Inheritance is one of the key features of Object-oriented programming in C++. It
allows us to create a new class (derived class) from an existing class (base class).
• The derived class inherits the features from the base class and can have additional
features of its own. For example
• Inheritance is one of the core feature of an object-oriented programming language.
It allows software developers to derive a new class from the existing class. The
derived class inherits the features of the base class (existing class).
• There are various models of inheritance in C++ programming.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multilevel Inheritance
• In C++ programming, not only you can derive a class from the base class
but you can also derive a class from the derived class. This form of
inheritance is known as multilevel inheritance.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multilevel Inheritance
• In C++ programming, not only you can derive a class from the base class
but you can also derive a class from the derived class. This form of
inheritance is known as multilevel inheritance.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multilevel Inheritance
• In C++ programming, not only you can derive a class from the base class
but you can also derive a class from the derived class. This form of
inheritance is known as multilevel inheritance.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multilevel Inheritance
• In C++ programming, not only you can derive a class from the base class
but you can also derive a class from the derived class. This form of
inheritance is known as multilevel inheritance.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multiple Inheritance
• In C++ programming, a class can be derived from more than one parent.
For example, A class Bat is derived from base classes Mammal and Winged
Animal. It makes sense because bat is a mammal as well as a winged animal.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multiple Inheritance
• In C++ programming, a class can be derived from more than one parent.
For example, A class Bat is derived from base classes Mammal and Winged
Animal. It makes sense because bat is a mammal as well as a winged animal.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Multiple Inheritance
• In C++ programming, a class can be derived from more than one parent.
For example, A class Bat is derived from base classes Mammal and Winged
Animal. It makes sense because bat is a mammal as well as a winged animal.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Hierarchical Inheritance
• If more than one class is inherited from the base class, it's known as Hierarchical
Inheritance. In hierarchical inheritance, all features that are common in child
classes are included in the base class.
• For example, Physics, Chemistry, Biology are derived from Science class. Similarly,
Dog, Cat, Horse are derived from Animal class.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Hierarchical Inheritance
• If more than one class is inherited from the base class, it's known as Hierarchical
Inheritance. In hierarchical inheritance, all features that are common in child
classes are included in the base class.
• For example, Physics, Chemistry, Biology are derived from Science class. Similarly,
Dog, Cat, Horse are derived from Animal class.
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Hierarchical Inheritance
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Hierarchical Inheritance
16.3.8 Illustrate Types Of Inheritance In C++
Programming Using Different Classes
• C++ Hierarchical Inheritance
16.3.9 Describe Polymorphism In C++
Programming
• Polymorphism is an important concept of object-oriented programming. It
simply means more than one form. That is, the same entity (function or operator)
behaves differently in different scenarios. For example,
• Think of a base class called animal that has a method called “animalSound()”.
• Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have
their own implementation of an animal sound (the pig oinks, and the cat meows,
etc.):
•
16.3.9 Describe Polymorphism In C++
Programming
16.3.9 Describe Polymorphism In C++
Programming
16.3.9 Describe Polymorphism In C++
Programming
16.3.10 Describe The Concept Of Abstraction
In C++ Programming
• Abstraction is the process of only showing the necessary details to the user and
hiding the other details in the background.
• Control and data are the two types of abstraction in C++.
• Abstraction in C++ is achieved through classes, header files, and access specifiers
(public, private, protected).
• Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerator will increase the speed of the car or applying brakes will
stop the car but he does not know how on pressing the accelerator the speed is
actually increasing, he does not know about the inner mechanism of the car or the
implementation of the accelerator, brakes, etc in the car. This is what abstraction
is.
16.3.10 Describe The Concept Of Abstraction
In C++ Programming
• Abstraction using Classes
• We can implement Abstraction in C++ using classes. The class helps us to
group data members and member functions using available access
specifiers. A Class can decide which data member will be visible to the
outside world and which is not.
16.3.10 Describe The Concept Of Abstraction
In C++ Programming
• Abstraction in Header files
• One more type of abstraction in C++ can be header files. For example,
consider the pow() method present in math.h header file. Whenever we
need to calculate the power of a number, we simply call the function pow()
present in the math.h header file and pass the numbers as arguments
without knowing the underlying algorithm according to which the function
is actually calculating the power of numbers.
16.3.10 Describe The Concept Of Abstraction
In C++ Programming
• Abstraction using Access Specifiers
• Access specifiers are the main pillar of implementing abstraction in C++. We can
use access specifiers to enforce restrictions on class members. For example:
• Members declared as public in a class can be accessed from anywhere in the
program.
• Members declared as private in a class, can be accessed only from within the class.
They are not allowed to be accessed from any part of the code outside the class.
16.3.11 Differentiate Between Overloading
And Overriding In OOP
• Function Overloading:
• Function overloading is a feature of object-oriented programming where
two or more functions can have the same name but different parameters.
When a function name is overloaded with different jobs it is called
Function Overloading. In Function Overloading “Function” name should
be the same and the arguments should be different. Function overloading
can be considered as an example of a polymorphism feature in C++.
16.3.11 Differentiate Between Overloading
And Overriding In OOP
• Function Overloading:
• The parameters should follow any one or more than one of the following
conditions for Function overloading:
• Parameters should have a different type
• It can be done in base as well as derived class.
• For example:
16.3.11 Differentiate Between Overloading
And Overriding In OOP
16.3.11 Differentiate Between Overloading
And Overriding In OOP
16.3.11 Differentiate Between Overloading
And Overriding In OOP
Output of the Program:
16.3.11 Differentiate Between Overloading
And Overriding In OOP
• Function Overriding:
• In the ‘overriding‘ prototype the overridden function is the same
throughout the program but, the function to be overridden is preceded by
the keyword ‘virtual’ in the base class and is redefined by the derived class
without any keyword.
16.3.11 Differentiate Between Overloading
And Overriding In OOP
• Function Overriding:
• As we know, inheritance is a feature of OOP that allows us to create derived
classes from a base class. The derived classes inherit features of the base class.
• Suppose, the same function is defined in both the derived class and the based
class. Now if we call this function using the object of the derived class, the
function of the derived class is executed.
• This is known as function overriding in C++. The function in derived class
overrides the function in base class.