[go: up one dir, main page]

0% found this document useful (0 votes)
38 views18 pages

OOP Unit 5

oop unit 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views18 pages

OOP Unit 5

oop unit 5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT-5

Objects and Classes


Object

 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 class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined in
the class, you need to create objects.

Syntax: ClassName ObjectName;

Classes:
is a user-defined data type, which holds its own data members and member functions,
which can be accessed and used by creating an instance of that class. A C++ class is
like a blueprint for an object.

1
Difference between object and class:

Class Object

Class is used as a template for


An object is an instance of a class.
declaring and creating the objects.

When a class is created, no memory is Objects are allocated memory space


allocated. whenever they are created.

The class has to be declared first and An object is created many times as per
only once. requirement.

A class cannot be manipulated as they


Objects can be manipulated.
are not available in the memory.

A class is a logical entity. An object is a physical entity.

It is created with a class name in C++


It is declared with the class keyword
and with the new keywords in Java.

Class does not contain any values


Each object has its own values, which are
which can be associated with the
associated with it.
field.

A class is used to bind data as well as


Objects are like a variable of the class.
methods together as a single unit.

Syntax: Instantiating an object for a Class


in C++ is as follows:
class Student {
public:
void put(){
Syntax: Declaring Class in C++ is as
cout<<“Function Called”<<endl;
follows:
}
class <classname> {};
};
int main(){
Student s1; // Object created
s1.put();
}

2
Class Object

Example: Bike Example: Ducati, Suzuki, Kawasaki

Private:

If the data members are declared as private access then they cannot be accessed from
other functions outside the class. It can only be accessed by the functions declared
within the class. It is declared by the key word „private‟

Public:
If the data members are declared public access then they can be accessed from other
functions outside the class. It is declared by the key word „public‟.

Differentiate between private and public members of a class in C++.

3
Private Public
members cannot be accessed from outside the members are accessible
class from outside the class
It provides high security of its data member It does not provide security for
its data member
It can’t be inherited The public member is
inherited from drive class

Constructors:

 Constructor is a special member function of a class that initializes the object of


the class. Constructor name is same as class name and it doesn’t have a return
type.
 In C++, Constructor is automatically called when object (instance of class)
create. It is special member function of the class.
A constructor is different from normal functions in following ways:

1. Constructor has same name as the class itself.


2. Constructors don't have return type.
3. A constructor is automatically called when an object is created.
4. If we do not specify a constructor, C++ compiler generates a default
5. Constructor for us (expects no parameters and has an empty body).

4
Example:

class MyClass {
public:
MyClass() // Constructor
{
cout << "Hello World!";
}
};
int main() {
MyClass myObj;
return 0;
}

Types of constructor:-
C++ has the following types of Constructors.

1. Default Constructor
2. Parameterized Constructor
3. Multiple Constructors
4. Constructor with default arguments
5. Copy Constructor

1. Default Constructor

A constructor without arguments is called default constructor.

Syntax for declaration of default constructor

class class_name
{
Public:
Class_name ( ) // default Constructor
{
}
};
int main()
{
Class_name object;
return 0;
}

5
2. Parameterized Constructor

 The constructors having arguments as parameters are known as


parameterized Constructors.
 These types of constructors are useful when the programmer wants to
initialize various data elements of different objects with different values
during their creation.

Syntax:

class class_name
{
Public:
Class_name (argument _list) // parameterized Constructor
{
}
};

Copy Constructor
Copy constructor is used to initialize an object from another object. In this way declaration of
constructor involves object of same class in its argument list.

Destructors in C++
A destructor is a special member function that works just opposite to constructor, unlike
constructors that are used for initializing an object, destructors destroy (or delete) the object.

Syntax:

6
Inheritance
The mechanism of deriving a new class from an old one is called inheritance.
In C++, the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is
the specialized class for the base class.

a. Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
b. Super Class: The class whose properties are inherited by sub class is called Base
Class or Super class.

7
Types of Inheritance

There are 5 types of inheritance in C++

1) Single inheritance

2) Multiple inheritances

3) Multilevel inheritance

4) Hierarchical inheritance

5) Hybrid inheritance

1) Single Inheritance:-

 One derived class inherits from only one base class. It is the most simplest
form of Inheritance.

8
2. Multiple Inheritances: In this type of inheritance a single derived class may inherit
from two or more than two base classes.

Syntax:

3. Multilevel Inheritance:

In this type of inheritance, a derived class is created from another derived class.

9
4.Hierarchical Inheritance

In this type of inheritance, more than one sub class is inherited from a single base
class. i.e. more than one derived class is created from a single base class.

5. Hybrid Inheritance: Hybrid inheritance is combination of two or more


inheritances such as single, multiple, multilevel or Hierarchical inheritances.

Polymorphism:

 The term "Polymorphism" is the combination of "poly" + "morphs" which


means many forms.
 In simple words, we can define polymorphism as the ability of a message to be
displayed in more than one form.
 Polymorphism is an object-oriented programming concept that refers to the
ability of a variable, function or object to take on multiple forms.

10
Types of Polymorphism: In C++ polymorphism is mainly divided into two types:
1. Compile time Polymorphism
a) Function or (Method) overloading

b) Operator overloading

2. Runtime Polymorphism
a) Virtual Function

1. Compile Time polymorphism: This type of polymorphism is achieved by


function overloading or operator overloading.

a. Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions can
be overloaded by change in number of arguments or/and change in type of
arguments.

Example: // C++ program for function overloading

11
b. Operator Overloading:

C++ also provides option to overload operators.

For example, we can make the operator (‘+’) for string class to concatenate
two strings. We know that this is the addition operator whose task is to add
two operands. So a single operator ‘+’when placed between integer operands ,
adds them and when placed between string operands, concatenates them.
Example: // CPP program to illustrate Operator Overloading

#include<iostream>

using namespace std;

class Complex {

private: int real, imag;

12
Output: 12 + i9

2. Runtime Polymorphism This type of polymorphism is achieved by Function


Overriding.

Static & Dynamic Binding

The overloaded member functions are „selected‟ for invoking by matching arguments,
both type and number. This information is known to the compiler at the compile time
and compiler is able to select the appropriate function for a particular call at the
compile time itself. This is called Early Binding or Static Binding or Static Linking.

13
Also known as compile time polymorphism. Early binding means that an object is
bound to its function call at the compile time.
It would be nice if the appropriate member function could be selected while the
program is
Running. This is known as runtime polymorphism. C++ supports a mechanism
known as virtual function to achieve run time polymorphism.
At the runtime, when it is known what class objects are under consideration, the
appropriate
version of the function is invoked. Since the function is linked with a particular class
much later after the compilation, this process is termed as late binding. It is also
known as dynamic binding because the selection of the appropriate function is done
dynamically at run time.

Function overriding on the other hand occurs when a derived class has a definition
for one of the member functions of the base class. That base function is said to be
overridden.

VIRTUAL FUNCTIONS
When we use the same function name in both the base and derived classes, the
function in the bas class is declared as virtual using the keyword virtual preceding its
normal declaration.

When a function is made virtual, C++ determines which function to use at runtime
based on the type of object pointed to by the base pointer, rather than the type of the
pointer. Thus, by making the base pointer to point to different objects, we can execute
different versions of the virtual function

#include<iostream>
Using namespace stu;
class Base
{
public:
void display()

14
{
cout<<”Display Base”;
}
virtual void show()
{
cout<<”Show Base”;
}
};

class Derived : public Base


{
public:
void display()
{
cout<<”Display Derived”;
}
void show()
{
cout<<”show derived”;
}
};

void main()
{
Base b;
Derived d;
Base *ptr;
ptr=&b;
ptr->display(); //calls Base
ptr->show(); //calls Base
ptr=&d;
ptr->display(); //calls Base
ptr->show(); //class Derived
}

15
Differences b/w Compile Time and Run time Polymorphism

Compile Time Polymorphism Run time Polymorphism

In Compile time Polymorphism, the call In Run time Polymorphism, the call is
is resolved by the compiler. not resolved by the compiler.

It is also known as Static binding, Early It is also known as Dynamic binding,


binding and overloading as well. Late binding and overriding as well.

Method overloading is the compile-time Method overriding is the runtime


polymorphism where more than one polymorphism having the same method
methods share the same name with with same parameters or signature but
different parameters or signature and associated withcompared, different
different return type. classes.

It is achieved by function overloading It is achieved by virtual functions and


and operator overloading. pointers.

It provides slow execution as compare


It provides fast execution because the
to early binding because the method
method that needs to be executed is
that needs to be executed is known at
known early at the compile time.
the runtime.

Compile time polymorphism is less Run time polymorphism is more


flexible as all things execute at compile flexible as all things execute at run
time. time.

Inheritance is not involved. Inheritance is involved.

16
What is the 'this' pointer?

In C++, This is a keyword that refers to the current instance of the class.

Example:

#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
void setdata (int x)
{
this->x = x;
}
void print()
{ cout << "x = " << x << endl; }
};
int main()
{
Test obj;
obj.setdata(20);
obj.print();
return 0;
}

17
Important questions of UNIT-5

1. Differentiate between Object and Class.


2. Differentiate between private and public members of a class in C++.
3. Discuss constructors and their types.
4. Explain this pointer.
5. Explain the concept of constructors in C++. Discuss different types of
constructors. Give example of each.
6. Define Polymorphism and give an example of its implementation in object-
oriented Programming.
7. Discuss operator overloading in C++.
8. Explain in detail about the concept, types and application of Polymorphism.
9. What does inheritance means in C++? What are different forms of inheritance ?
Give an example of each.
10. Explain inheritance with its different types. Write a program in C++ to
demonstrate multiple inheritances.
11. Compare virtual and pure virtual functions with example
12. Differentiate between constructors and destructors. Explain the use of
constructors and destructor with suitable examples. Discuss copy constructor
with code.

18

You might also like