You are on page 1of 15

CLASS & OBJECTS

Presented By:-
AKSHAT GUPTA
BCA209
35225502019
An Introduction To Classes
 A class is a building block of OOP. It is the
way to bind the data and its logically related
functions together.
 An abstract data type that can be treated
like any other built in data type.
Class Definition
 Class head class name_of_class.
 Class body {

};
Class Syntax
class classname{
Access specifier :
Member variable Declaration;
Member Function Declaration;
};
#include<iostream>
Example using namespace std;
class maths{
int x,y;
public:
void input() //creating a member function to take
input from the user
{
cout<<"Enter two integers : \n";
cin>>x>>y;
}
void add() //creating a member function to perform
addition of the entered no.
{
cout<<"Result : "<<x+y;
}
};
Access Specifiers & Its Types
Access specifiers define how the members
(attributes & methods) of a class can be accessed.
In C++ there are three types of access specifiers:-
1.Public.
2.Private.
3.Protected.
Access Specifiers & Its Types(Contd.)

1.Public : - It is the first type of access specifiers


in C++, which allows the members of the class
to be accessed from outside the class.
Example: class m{//the class
public: // access specifier
//class members };
Access Specifiers & Its Types(Contd.)

2.Private : - The second type of the access


specifier in C++ is Private. In this the members
cannot be accessed (or viewed) from outside the
class.
Example: class m{//the class
private: // access specifier
//class members };
Access Specifiers & Its Types(Contd.)

3.Protected : - The third & last access specifier in C+


+ is Protected. It does not allows the members to be
accessed from outside the class, however,
they can be accessed in inherited classes.
Example: class m{//the class
protected: // access specifier
//class members };
Member Function and Its Features
Member function of a class is a function that has its
definition or its prototype within the class definition
like any other variable.
Features : -
1. Member function’s name is visible outside the
class.
2. It can be defined inside or outside the class.
3. It can have access to private, public and
protected data members of its class, but cannot
access private data members of another class.
Introduction To Objects
Object is an abstraction of real world entity. It is also
known as the variables / instances of classes.
Syntax for declaring objects is as follows :
<class name>
<obj_name1>,<obj_name2>, …, <obj_name1>;

Example: class c{//it is class named as c};


int main(){c t1,t2,…tn;//objects created for
the class }
Features Of Objects
1.It can have its own copy of data members.
2. The scope of an object is determined by the place
in which the object is defined.
3.It can be passed to a function like normal
variables.
4. The members of the class can be accessed by
the object using the object to member access
operator or dot operator(.).
Example int main()
{
#include<iostream> maths m;//creating an object of
using namespace std; the class
class maths{ m.input();//calling the function
int x,y; input using object m
public: m.add();//calling the function
void input() //creating a member function to add using object m
take input from the user return 0;
{cout<<"Enter two integers : \n“;
}
cin>>x>>y; }
void add() //creating a member function to
Output:
perform addition of the entered no. and print
Enter two integers :
the output
2
{
5
cout<<"Result : "<<x+y;
Result :7
}
};
REFERENCES

https://www.geeksforgeeks.org/c-classes-and-objects/

https://www.w3schools.com/cpp/cpp_classes.asp

https://www.slideshare.net/PayelGuria/class-and-objects
THANK YOU!

You might also like