Class and Object Cpp New
Class and Object Cpp New
Class: A class in C++ is the building block that leads to Object-Oriented programming. 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. A C++ class is like a blueprint
for an object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In
other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality. Object is a runtime entity, it is created at runtime. Object is
an instance of a class.
Create a Class
Example
class Abc
private:
int x;
public:
void display()
{
Alphabet Computer Education, Narayangaon, 9960552002
// some statement
};
int main()
Now before studying how to define class and its objects, lets first quickly learn what are
access modifiers.
Access modifiers in C++ class defines the access control rules. C++ has 3 new keywords
introduced, namely,
1. public
2. private
3. protected
These access modifiers are used to set boundaries for availability of members of class be it
data members or member functions
Public, means all the class members declared under public will be available to everyone. The
data members and member functions declared public can be accessed by other classes too.
class PublicAccess
public:
Alphabet Computer Education, Narayangaon, 9960552002
Private keyword, means that no one can access the class members declared private, outside
that class. If someone tries to access the private members of a class, they will get a compile
time error. By default class variables and member functions are private.
class PrivateAccess
private:
Protected, is the last access specifier, and it is similar to private, it makes class member
inaccessible outside the class. But they can be accessed by any subclass of that class. (If
class A is inherited by class B, then class B is subclass of class A. We will learn about
inheritance later.)
class ProtectedAccess
protected:
Defining Class and Creating Objects for student inside class definition
#include<iostream.h>
#include<conio.h>
class student
private:
char name[20],regd[10],branch[10];
int sem;
public:
void input()
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regdno.:";
cin>>regd;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
}
Alphabet Computer Education, Narayangaon, 9960552002
void display()
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
};
void main()
student s;
s.input();
s.display();
getch();
Sample Input
Enter Name:Bikash
Enter Regdno.:123
Enter Branch:CS
Enter Sem:5
Sample Output
Name:Bikash
Regdno.:123
Branch:CS
Sem:5
Defining Class and Creating Objects for student outside class definition
Alphabet Computer Education, Narayangaon, 9960552002
#include<iostream.h>
#include<conio.h>
class student
private:
char name[20],regd[10],branch[10];
int sem;
public:
void input();
void display();
};
void student::input()
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regdno.:";
cin>>regd;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
void student::display()
Alphabet Computer Education, Narayangaon, 9960552002
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
int main()
student s;
s.input();
s.display();
getch();
Data members:
1) Name of the depositor
2) Account number
3) Type of account
4) Balance amount in the account.
Member functions:
1) To assign initial values
2) To deposit an amount
3) To withdraw an amount after checking the balance
4) To display name and balance.
Alphabet Computer Education, Narayangaon, 9960552002
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
class bank
int acno;
float balance;
public:
void input()
cin>>acno;
cin>>name;
cin>>acctype;
cin>>balance;
Alphabet Computer Education, Narayangaon, 9960552002
void deposit();
void withdraw();
void display();
};
void bank::deposit()
int damt1;
cin>>damt1;
balance+=damt1;
void bank::withdraw()
int wamt1;
cin>>wamt1;
if(wamt1>balance)
balance-=wamt1;
void bank::display()
{
Alphabet Computer Education, Narayangaon, 9960552002
cout<<"\n ----------------------";
int main()
bank b1;
clrscr();
b1.input();
b1.deposit();
b1.withdraw();
b1.display();
getch();
return 0;
Following is an example to show you how to initialize and use the public data members using
the dot (.) operator and the respective object of class.
#include<iostream.h>
Alphabet Computer Education, Narayangaon, 9960552002
#include<conio.h>
class Student
public:
int rollno;
};
int main()
Student A;
Student B;
A.rollno=1;
B.rollno=2;
Roll no of A is: 1
Roll no of B is: 2
To access, use and initialize the private data member you need to create getter and setter
functions, to get and set the value of the data member.
The setter function will set the value passed as argument to the private data member, and
the getter function will return the value of the private data member to be used. Both
getter and setter function must be defined public.
Alphabet Computer Education, Narayangaon, 9960552002
#include<iostream.h>
#include<conio.h>
class Student
int rollno;
int getRollno()
return rollno;
void setRollno(int i)
rollno=i;
};
int main()
Student A;
A.setRollno(26);
Write a C++ program to implement a class called Circle that has private
member variables for radius. Include member functions to calculate the circle's
area
#include <iostream.h>
#include <conio.h>
#include <math.h>
class Circle {
private:
double radius;
public:
radius=rad;
}
Alphabet Computer Education, Narayangaon, 9960552002
double calculateArea()
};
int main()
double radius;
Circle c;
clrscr();
c.input(radius);
getch();
return 0;
#include<iostream.h>
#include<conio.h>
class Employee
int Id;
char Name[25];
int Age;
long Salary;
public:
cin>>Id;
cin>>Name;
cin>>Age;
cin>>Salary;
}
Alphabet Computer Education, Narayangaon, 9960552002
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
};
void main()
int i;
Employee E[3];
clrscr();
for(i=0;i<3;i++)
E[i].GetData();
cout<<"\nDetails of Employees";
for(i=0;i<3;i++)
E[i].PutData();
getch();
Output :
Details of Employees
We already know what member functions are, what they do, how to define member function
and how to call them using class objects. Now let’s learn about some special member
functions which can be defined in C++ classes. Following are the different types of Member
functions:
Simple functions
Static functions
Const functions
Inline functions
Friend functions
These are the basic member function, which dont have any special keyword like static etc
as prefix. All the general member functions, which are of below given form, are termed as
simple and basic member functions.
return_type functionName(parameter_list)
function body;
#include <iostream>
#include<conio.h>
class Employee
int id;
float salary;
Alphabet Computer Education, Narayangaon, 9960552002
public:
void insert()
cin>>Id;
cin>>Salary;
void display()
};
int main(void)
Employee e1;
clrscr();
e1.insert();
e1.display();
getch();
return 0;
#include <iostream>
#include<conio.h>
class Employee
int id;
float salary;
public:
void insert()
cin>>Id;
cin>>salary;
int returnid()
return id;
float returnsalary()
return salary;
};
int main(void)
Alphabet Computer Education, Narayangaon, 9960552002
Employee e1;
clrscr();
e1.insert();
getch();
return 0;
#include <iostream>
#include<conio.h>
class Employee
public:
int id;
float salary;
id = i;
salary = s;
void display()
};
int main(void)
Employee e1;
clrscr();
e1.insert(201, 990000);
e1.display();
getch();
return 0;
#include <iostream>
#include<conio.h>
class Employee
public:
int id;
float salary;
id = i;
salary = s;
}
Alphabet Computer Education, Narayangaon, 9960552002
int returnid()
return id;
float returnsalary()
return salary;
};
int main(void)
Employee e1;
clrscr();
e1.insert(201, 990000);
getch();
return 0;
with function name. These functions work for the class as whole rather than for a particular
object of a class.
It can be called using the object and the direct member access But, its more typical to call
a static member function by itself, using class name and scope resolution :: operator.
#include <iostream>
#include<conio.h>
class X
public:
};
int main()
X::f();
These functions cannot access ordinary data members and member functions, but
only static data members and static member functions can be called inside them.
// statement
#include <iostream>
#include<conio.h>
class Demo {
int x;
public:
void set_data(int a)
x = a;
int get_data()
++x;
return x;
Alphabet Computer Education, Narayangaon, 9960552002
};
main()
Demo d;
clrscr();
d.set_data(10);
getch();
return 0;
Output
11
#include <iostream>
#include<conio.h>
class Demo {
int x;
public:
Alphabet Computer Education, Narayangaon, 9960552002
void set_data(int a)
x = a;
// member
++x;
return x;
};
main()
Demo d;
clrscr();
d.set_data(10);
getch();
return 0;
Output
++x;
#include <iostream>
#include<conio.h>
class Demo
int x;
public:
void set_data(int);
};
void Demo::set_data(int a)
x = a;
return x;
}
Alphabet Computer Education, Narayangaon, 9960552002
main()
Demo d;
d.set_data(10);
getch();
return 0;
Output
10
One of the key features of C++ is the inline function. Therefore, let's first examine the
utilization of inline functions and their intended application. If make a function is inline,
then the compiler replaces the function calling location with the definition of the inline
function at compile time.
Any changes made to an inline function will require the inline function to be recompiled
again because the compiler would need to replace all the code with a new code; otherwise,
it will execute the old functionality.
// function code
#include<iostream.h>
Alphabet Computer Education, Narayangaon, 9960552002
#include<conio.h>
return s*s*s;
int main()
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
class operation
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
Alphabet Computer Education, Narayangaon, 9960552002
cin >> a;
cin >> b;
add = a+b;
sub = a-b;
mul = a*b;
div=a/b;
int main()
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
Output: