OOPS Classes and Objects
OOPS Classes and Objects
07/18/2024
AIM:
OOPs refers to languages that use objects in programming. It aims to implement real-
world entities like inheritance, hiding, polymorphism, etc in programming.
The main aim of OOP is to bind together the data and the functions that operate on them
so that no other part of the code can access this data except that function.
07/18/2024
• Basically, the C++ language is the C language with more syntax added. This makes
C++ able to compile all C programs and use C libraries as well.
• This makes C++ a both fast and modern language, however, it's still considered a
difficult one and as one which is used mainly for specific applications.
07/18/2024
• It 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.
07/18/2024
07/18/2024
• 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.
07/18/2024
• 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 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 power
of numbers.
07/18/2024
cout<<"Char
OOPs Using C++ is: "<<ch<<endl; 9
} };
Polymorphism
For Example:
A real-life example of polymorphism, a person at the same time can have different
characteristics. Like a man at the same time is a father, a husband, an employee. So
the same person posses different behavior in different situations. This is called
polymorphism.
07/18/2024
• Inheritance is a process in which one object acquires all the properties and behaviors
of its parent object automatically.
• In such way, the developer can reuse, extend or modify the attributes and behaviors
which are defined in other class.
For Example:
07/18/2024
07/18/2024
07/18/2024
Creating an object
07/18/2024
Output:
07/18/2024
07/18/2024
07/18/2024
Access Specifier
Output:
07/18/2024
Access Specifier
Output: Error
07/18/2024
Access Specifier
Output:
07/18/2024
07/18/2024
Output:
07/18/2024
• The way the memory space for data members and member functions is allocated is
different regardless of the fact that both data members and member functions belong to
the same class.
• The memory space is allocated to the data members of a class only when an object of
the class is declared, and not when the data members are declared inside the class.
• Since a single data member can have different values for different objects at the same
time, every object declared for the class has an individual copy of all the data members.
07/18/2024
• On the other hand, the memory space for the member functions is allocated only
once when the class is defined.
• In other words, there is only a single copy of each member function, which is shared
among all the objects.
• For instance, the two objects, namely, obj1, and obj2 of the class Counter have
individual copies of the data member count.
• However, there is only one copy of the member functions setData (), getData () and
inc() that is shared by all the two objects.
07/18/2024
07/18/2024
07/18/2024
?
OOPs Using C++ 29
Output
07/18/2024
07/18/2024
07/18/2024
07/18/2024
?
OOPs Using C++ 33
Output
Output:
07/18/2024
34
Constant Member Function and Object
• A function becomes const when the const keyword is used in the function’s
declaration.
Syntax:
datatype function_name const();
• The idea of const functions is not to allow them to modify the object on which they
are called.
• When a function is declared as const, it can be called on any type of object, const
object as well as non-const objects.
Note: When a function is declared as const, it can be called on any type of object.
Non-const functions can only be called by non-const objects.
07/18/2024
Output:
07/18/2024
07/18/2024
37
Guess Output
07/18/2024
?
OOPs Using C++ 38
Output
Why?
• A function becomes const when the const keyword is used in the function’s
declaration.
• The idea of const functions is not to allow them to modify the object on which
they are called.
07/18/2024
39
Example
When a function is declared as const, it can be called on any type of object. Non-const
functions can only be called by non-const objects.
Example:
int main()
class Test {
{
public:
//Constant Object
void print() const
const Test t1;
{
t1.print();
cout<<"Constant Function\n";
//Non-Constant Object
}
Test t2;
};
t2.print();
return 0;
Output:
}
07/18/2024
• If a function is defined as a friend function in C++, then the protected and private data of a
class can be accessed using the function. A global function can also be declared as friend.
• By using the keyword friend compiler knows the given function is a friend function.
• For accessing the data, the declaration of a friend function should be done inside the body
of a class starting with the keyword friend.
• class class_name
• {
• friend data_type function_name(argument/s); // syntax of friend function.
• };
• In the above declaration, the friend function is preceded by the keyword friend. The
function can be defined anywhere in the program like a normal C++ function. The function
definition does not use either the keyword friend or scope resolution operator.
07/18/2024
41
Friend Function
• Characteristics of a Friend function:
• The function is not in the scope of the class to which it has been declared as a friend.
• It cannot be called using the object as it is not in the scope of that class.
• It can be invoked like a normal function without using the object.
• It cannot access the member names directly and has to use an object name and dot
membership operator with the member name.
• It can be declared either in the private or the public part.
07/18/2024
42
Example
• In this example we have two classes XYZ and ABC. The XYZ class has two private data
members ch and num, this class declares ABC as friend class.
• This means that ABC can access the private members of XYZ, the same has been
demonstrated in the example where the function disp() of ABC class accesses the private
members num and ch.
• In this example we are passing object as an argument to the function.
07/18/2024
45
Passing Object as an argument
• Passing object by value to a function creates a shallow local copy of the object in the
function scope. Things you modify here won't be reflected in the object passed to it.
int main() {
#include <bits/stdc++.h>
Example E1, E2, E3; //objects created
using namespace std; // Values are initialized for both objects
class Example { E1.a = 50;
public: E2.a = 100;
int a; E3.a = 0;
cout << "Initial Values \n";
// This function will take object as arguments and return cout << "Value of object 1: " << E1.a
object << ", \nobject 2: " << E2.a
Example add(Example Ea, Example Eb) << ", \nobject 3: " << E3.a
{ << "\n"
Example Ec; E3 = E3.add(E1, E2); // Passing object as an argument to function
Ec.a = Ea.a + Eb.a; add()
// Changed values after passing object as an argument
return Ec; // returning the object
cout << "New values \n";
} };
07/18/2024
46
Passing Object as an argument
07/18/2024
47
Passing Object as reference
• This passes a reference to the object to the function. Things you modify here will be
reflected in the object passed to it. No copy of the object is created.
• Declaration #include <bits/stdc++.h>
using namespace std;
• void fun(X &x);
class Example {
• Calling public:
• X x; int a;
• fun(x); // This function will take reference to object as arguments and return
object
Example add(Example &Ea, Example &Eb) // reference of object E1 and
E2 created as Ea and Eb
{
Example Ec;
Ec.a = Ea.a + Eb.a;
Ea.a = Ec.a;
07/18/2024 Eb.a = Ec.a;
return Ec; // returning the object
48
}
Example
int main()
// Passing object as an argument to function add()
{ E3 = E3.add(E1, E2);
Example E1, E2, E3; // Changed values after passing object as an argument
// Values are initialized for both objects cout << "New values \n";
E1.a = 50; cout << "Value of object 1: " << E1.a
E2.a = 100; << ", \nobject 2: " << E2.a
E3.a = 0; << ", \nobject 3: " << E3.a
cout << "Initial Values \n"; << "\n";
cout << "Value of object 1: " << E1.a return 0;
}
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";
07/18/2024
49
Passing Object as address
• This passes the address of object as a pointer to the function. This is similar to passing
a reference to the object. No copy of object is created.
• Declaration
• Calling
• X x;
• fun(&x);
07/18/2024
50
Default arguments
• If a function with default arguments is called without passing arguments, then the default
parameters are used.
• However, if arguments are passed while calling the function, the default arguments are
ignored.
07/18/2024
51
Example
07/18/2024
52
Default arguments
• When temp() is called, both the default parameters are used by the function.
• When temp(6) is called, the first argument becomes 6 while the default value is
used for the second parameter.
• When temp(6, -2.3) is called, both the default parameters are overridden,
resulting in i = 6 and f = -2.3.
• When temp(3.4) is passed, the function behaves in an undesired way because the
second argument cannot be passed without passing the first argument.
• Therefore, 3.4 is passed as the first argument. Since the first argument has been
defined as int, the value that is actually passed is 3.
07/18/2024
53
THANK YOU
07/18/2024
55