Class, Objects, defining member functions, Arrays of Objects, Pointers and classes,
Static Data Members, Static Member Functions, Passing Objects as Function
Arguments, constructors, types of constructors, destructors, this pointer, Access
Specifier , Friend Functions, Inline functions.
Introduction of Class
An object oriented programming approach is a collection of objects and each
object consists of corresponding data members and member function.
Oops makes the program reusable and more maintainable. The important aspect in
oop is a class which has similar syntax that of structure.
class:
➢ It can also called as blue print or prototype that defines the variables and
functions common to all objects of certain kind.
➢ It is also known as user defined data type or ADT(abstract data type).
➢ A class is declared by the keyword class.
➢ Class is a product of Encapsulation and Data Abstraction of real world
object. Encapsulation means combine data and function into single unit and
data abstraction means declare members of the class as public, private and
protected.
➢ It is a collection of data and member functions that manipulate data.
Syntax:
, Object
In C++, an object is an instance of a class. Using object we can access data and
function using the dot operator (.).
Memory Allocation for Objects:
Memory for objects is allocated when they are declared but not when class is
defined.
All objects in a given class uses same member functions. The member functions
are created and placed in memory only once when they are defined in class
definition.
Memory is allocated to the object by run time or compiler when it is created.
Size of memory depends on sum of all the data members of the class.
There are few ways to create an object:
• Creating objects at the time of class declaration:
Class person
{
Public:
String name;
int age;
}p1,p2;
Here, p1,p2 are objects of person class.
• Using instance of the class (Static)
Syntax :
Classname variables;
Person obj;
Here, obj is an object of the class
• Using new operator(Dynamic)
Person *ptr=new Person();
This creates an object of type Person and returns a pointer to it.
The arrow operator (->) is used to access the member variable and functions of the
object.
,ptr->name=”RAM”;
ptr->age=30;
Access Specifier
Access specifier or access modifiers are the labels that specify type of access given
to members of a class. These are used for data hiding. These are also called as
visibility modes.
By using access specifiers, a class can control which members are accessible from
outside the class and which are not. They ensures the integrity of the class
implementation.
There are three types of access specifiers 1.private 2.public 3.protected
1.Private: If the data members are declared as private access then they cannot be
accessed outside the class, not even by objects of the class. It can only be accessed
by the functions declared within the class. It is declared by the key word “private” .
2.public: If the data members are declared public access then they can be accessed
anywhere in the program and from other functions outside the class. It is declared
by the key word “public”.
3.protected: The access level of protected declaration lies between public and
private. Its access properties are similar to private members. This access specifier
is used at the time of inheritance, they can be accessed by derived class.
Note:- 1.If the access specifier is not specified in the class the default access
specifier is private.
2.All member functions are to be declared as public if not they are not accessible
outside the class.
Example
#include<iostream>
#include<string>
using namespace std;
class student
{
private:
int roll;
, string name;
protected:
int age;
public:
void getdata()
{cout<<"Enter Roll number:";
cin>>roll;
cout<<"Enter Name:";
cin>>name;
}
void putdata()
{cout<<"Roll no:"<<roll<<endl;
cout<<"Name:"<<name<<endl;
cout<<"AGE :"<<age;
}
void protected_member()
{
cout<<"Enter age:";
cin>>age;
}
};
main()
{
student s;
s.getdata();
s.protected_member();
s.putdata();
}
OUTPUT :
Enter Roll number:22
Enter Name:RAM
Enter age:32
Roll no:22
Name:RAM
AGE :32