OOPS Notes - Unit-3
OOPS Notes - Unit-3
Credits: 4
• int a[8];
45 63 75 83
45 63 75 83
D B B A
Classes and Objects >> Re-visiting Structures in “C” >> Structures
45 63 75 83
D B B A
Classes and Objects >> Re-visiting Structures in “C” >> Structures
Example: I have a garage and I want to store all the information about a car available in my garage.
Car1
Car2
• Suppose you have 100 cars then storing all information in separate variable is not a good idea as it will
be time consuming and memory consuming.
• Array has the capability to store 100 elements but in this case, it is also not a good option.
Classes and Objects >> Re-visiting Structures in “C” >> Structures
Structure: It is a user defined datatype, that can be used to group elements of different types into a single type
Car1
Car2
Classes and Objects >> Re-visiting Structures in “C” >> Structures
We can access the members of the structure by using dot (.) operator
Classes and Objects >> Re-visiting Structures in “C” >> Structures
Classes and Objects >> Re-visiting Structures in “C” >> Approach
When to use:
•Use a structure when you want to group related data together (e.g., a student's name, age, and grade).
•Use an array when you have a collection of similar data items (e.g., names, ages, or grades of multiple students).
Classes and Objects >> Re-visiting Structures in “C” >> Limitations
Limitations
• The standard C does not allow struct data type to be treated like a built-in type
struct complex
{
float x;
float y;
};
(b) A class is a user-defined data type that binds data and functions that operate on data together in a single unit.
The syntax for defining a C++ class is as follows
Access Specifiers
Classes and Objects >> Defining a CLASS
The variables and functions declared within the curly braces are collectively known as members of the class.
while the functions declared in the class are known as “member functions”.
Example:
Classes and Objects >> Access Specifiers
• The keywords private, public and protected are known as access specifiers (also known as visibility mode). Each
member of a class is associated with an access specifier.
• The access specifier of a member controls its accessibility as well as determines the part of the program that can directly
access the member of the class.
• When a member is declared private, it can be accessed only inside the class,
• When a member is declared private, it is accessible both inside and outside the class.
• Protected members are accessible both inside the class in which they are declared as well as inside the derived classes of
the same class
Note that the default access specifier of the members of a class is private. That is, if no access specifier is provided in
the class definition, the access specifier is considered to be private.
Public:
All the class members declared under the public specifier will be available
to everyone.
In the above program, the data member radius is declared as public so it Output
could be accessed outside the class and thus was allowed access from
inside main().
Classes and Objects >> Access Specifiers >> Private
Private:
The class members declared as private can be accessed only by the member
functions inside the class. They are not allowed to be accessed directly by any
object or function outside the class.
Only the member functions or the friend functions are allowed to access the
private data members of the class.
The output of the above program is a compile time error because we are not
allowed to access the private data members of a class directly from outside the
class.
Output
However, we can access the private data members of a class indirectly using the
public member functions of the class.
Classes and Objects >> Access Specifiers >> Private
Output
Classes and Objects >> Defining an Object
Once a class is defined, it can be used to create variables of its type known as objects. The relation between an object and a
class is the same as that of a variable and its data type. Since an object is an instance of a class, the process of declaring an
object of a class is known as instantiation. The syntax for declaring an object is as follows
Classes and Objects >> Defining Member Functions >> Outside Class
Member functions of a class can be defined either outside the class definition or inside the class definition. In both cases,
the function body remains the same; however, the function header is different
Member functions of a class that are declared inside a class have to be defined separately outside the class.
• The membership label <class-name> :: tells the compiler that the function <function name> belongs to <class-name>
For example, consider the member functions getdata( ) and putdata( ) in earlier slides. They may be coded as follows:
• Since these functions do not return any value their datatype is void.
• Several different classes can use the same function name. the membership label will resolve their scope.
Another way to define a member function is to replace the function declaration by actual function defined inside a class.
class item
{
int number;
float cost;
Public:
void getdata (int a , float b);
• When a member function is defined inside a class, it is treated like an inline function.
• Therefore, only small functions are defined inside a class.
• A member function of a class can be called only by an object of that class using dot operator.
• However, A member function can be called by using its name inside another function of the same class.
This is known as nesting of member function.
• It is normal practice to place all the data items in a private section and all the functions public
• Some situation require functions to be hidden. E.g. Deleting an account in customer file, providing increment to
an employee. We can place this functions in private section.
class sample
{
int m;
void read(void);
Public:
void update (void);
void write(void);
};
// however private function can be called by its member function of same class
• We can have arrays of variables that are of the type class. Such variables are called array of objects.
consider this example.
class employee
• The identifier employee is a user defined datatype and can be used to create
{ objects that relate to different category of the employees.
Char name [30]; employee manager [3]; //array of manager
Float age;
employee foreman [15]; //array of foreman
public: employee worker [75]; //array of worker
void getdata (void);
void putdata(void);
• The array manager contains three objects, namely manager [0], manager
}; [1] and manager [2] of type employee class.
Since array of objects behaves like any other array, we can use the usual array accessing methods to access
individual elements and then the dot operator to access the member functions. For example.
this statement will display the data of ith element of array manager. That is this statement requests object manager
[i] to invoke the member function putdata ( )
• An array of object is stored inside the memory in the same way as a multidimensional array.
Classes and Objects >> Array of Objects >> example
Input output
• We studied that memory space for objects is allocated when they are declared and not when the class is
specified. This statement is partly true.
• Actually, the member functions are created and placed in the memory space only once when they are defined
as a part of a class specification.
• Since all the objects belonging to that class use the same member functions, no separate space is allocated for
member functions when the objects are created. Only space for member variables is allocated separately for
each object.
• Separate memory allocation for the object are essential because the member variables will hold different data
values for different objects.
Classes and Objects >> Memory Allocation for objects
Member function 1
Member function 2
Static data members are class members that are declared using static keywords. A static member has certain
special characteristics which are as follows:
•It is initialized to zero whenever first object of its class is created. No other initialization is permitted.
•For making any data member static we use the keyword “static”
•Only one copy of that member is created for the entire class and is shared by all the objects of that class, no
matter how many objects are created.
•Its lifetime is the entire program.
• The type and scope of each static member variable must be defined just outside the class definition.
int item : : count; //definition of static data member outside the class
Classes and Objects >> Static Data Member
• This is necessary because, the static data members are stored separately rather than aa a part of object.
• Since they are associated with the class itself rather than any class object, they are also called class variables.
class demo int demo : : z; // need to mention this line
{
int x, y; // normal data members int main ( )
static int z; // static data members {
demo aa, bb;
Public: aa.getdata (5, 10);
void getdata (int a, int b) bb. Getdata (12, 16);
{
x = a; aa.putdata( );
y = b; bb.putdata ();
z = z+1; };
}
void putdata ()
{ Output:
cout<< “x = ” << x << “\n=“ << y << “\n=“ << z;
} x =5 , y = 10, z = 2;
x = 12, y = 16, z = 2;
};
Classes and Objects >> Static Member Function
Like static data member, we can have static member function also. A member function that is declared static has
the following properties;
•A static member function can have access to only other static members (variables or functions).
•A static member function can be called using the class name (instead of its objects) For making any member
function static we use the keyword “static”
• As we know that, we can pass any type of arguments within the member function and there are any numbers of
arguments.
• In C++ programming language, we can also pass an object as an argument within the member function of class.
• This is useful, when we want to initialize all data members of an object with another object, we can pass objects
and assign the values of supplied object to the current object. For complex or large projects, we need to use
objects as an argument or parameter.
Classes and Objects >> objects as function arguments
Suppose, there is a function which is not a part of demo class, but still, we want to give it
access the utilize the resources of demo class. This type of function is called as friend
function.
Classes and Objects >> Friend functions
Key Points:
• 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.
• 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.
• A friend function cannot be called using the object of the class. It is called like a normal
function.
• A friend function can use the resources of the class only using an object of the same class.
{
cout << “enter two numbers”
cin >> a >> b
}
Suppose we have two class, Class A with object x and Class B with object y. If we want to
compare x and y then how we can do that??
Suppose you are implementing a simple 2D vector class in C++. Define a class
named Vector2D that represents a 2D vector with x and y components. Implement
member functions to perform vector addition, subtraction, scalar multiplication,
and dot product.
Now, to compute the angle between two vectors, you need to access the private x
and y components of both vectors. Implement a friend function named
computeAngle() outside the Vector2D class that takes two Vector2D objects as
parameters and calculates the angle between them.
Ensure to provide necessary validations, such as checking for zero vector, for the
angle calculation.
Classes and Objects >> Friend functions