Class and object
Class and object
Programming in C++
Nepal College of Information Technology
Course Instructor: Er. Rabina Chaudhary
Introduction of Class:
• Class is user defined data type that is used to specify data
representation and methods for manipulating the data in one
package.
• The data and functions within a class are called members of the class.
• When you define a class, you define blueprint for a data type.
• Class defines what an object of class will contain and what operations
can be performed in it.
• The data members and member functions can be grouped in private,
public or protected section.
Class:
• Syntax for defining a class; • Example:
class class_name class Student
{ {
access specifier: private:
variable_declaration; char name[20];
function_declaration; int roll;
access specifier: int marks;
variable_declaration; public:
function_declaration; void getDetails();
}; void display();
};
Data hiding in C++
• Data hiding is technique used in object oriented programming to hide object
details i.e. data members to limit access to data and prevent them from
unwanted manipulation
• There are three access specifiers:
i. private
ii. public
iii. protected
i. Private access specifier:
• If the class members are declared private, then they can be accesses
by member functions of that class only.
• Data members are made private to prevent direct access from outside
the class.
ii. Public access specifier:
• If the class members are declared public then they can be accessed from
anywhere in the program.
• Member functions are usually public which is used to manipulate the data
present in the class.
getdetails() display()
s1 s2
name roll marks name roll marks
Accessing class members:
• The private members of a class can be accessed only through
members of same class
• The public members of a class can be accessed from outside the class
using object name and dot operator
Syntax for accessing public data member:
object_name.data_member;
Syntax for accessing public member functions:
object_name.function_name(arguments);
Accessing class members: Example
class Student void main()
{
{
private:
char name[20]; Student s1;
int roll; s1.getDetails();
int marks; s1.display();
public:
void getDetails();
void display(); s1.roll=14578; //invalid as
}; roll is private member
Accessing class members: Example
class Student void main()
{
{
private:
char name[20]; Student s1;
int marks; s1.getDetails();
public: s1.display();
int roll;
void getDetails();
void display(); s1.roll=14578; valid as roll is
}; public member
Defining member function:
1. Inside the class body
2. Outside the class body
Syntax:
return_type class_name :: function_name(arguments)
{
function body
}
Defining member function: outside class body
class Student void Student :: getDetails()
{ {
private: cout<<“Enter details”;
char name[20]; cin>>name>>roll>>marks;
int roll; }
int marks; void Student :: display()
public: {
void getDetails(); cout<<“The name is “<<name<<endl;
void display(); cout<<“Roll is “<<roll<<endl;
}; cout<<“Marks is “<<marks<<endl;
}
Example Program:
// class and object example program
//member function defined inside class body
#include<conio.h>
#include<iostream.h> void display()
{
class Student cout<<endl<<endl<<"The Details are :"<<endl;
{ cout<<"The name is "<<name<<endl;
private: cout<<"Roll is "<<roll<<endl;
char name[20]; cout<<"Marks is "<<marks<<endl;
int roll; }
int marks; };
public:
void getDetails()
{
cout<<"Enter name of student : ";
cin>>name;
cout<<"Enter roll number : ";
cin>>roll;
cout<<"Enter marks obtained :";
cin>>marks;
}
Program continued…
class Student
{
private:
char name[20];
int roll;
int marks;
public:
void getDetails(); //member function declaration inside class.
void display();
};
Program continued…
Example:
Student S[50];
Array of Objects : Example Program
Student S[4];
class Student
{
private:
char name[20];
int roll;
int marks;
public:
void getDetails(int);
void display();
};
Program continued…
void Student:: getDetails(int x)
{
cout<<endl<<"Enter details of student "<<x<<" : "<<endl;
cout<<"Name : ";
cin>>name;
cout<<"Roll number : ";
cin>>roll;
cout<<"Marks obtained :";
cin>>marks;
cout<<endl<<endl;
}
void Student :: display()
{
cout<<"The name is "<<name<<endl;
cout<<"Roll is "<<roll<<endl;
cout<<"Marks is "<<marks<<endl<<endl;
}
Program Continued…
void main()
{
Student S[50];
int i,n;
cout<<"Enter number of Students : ";
cin>>n;
cout<<endl<<endl<<"Enter details of students"<<endl;
for(i=0;i<n;i++)
{
S[i].getDetails(i+1);
}
cout<<endl<<endl<<"Details of student are :"<<endl;
for(i=0;i<n;i++)
{
S[i].display();
}
getch();
}
Practice:
Q1. Write a program to define a class named Employee with its data
members name, salary, id number. Read records of n number of
employees and display the records.