Experiment No.3: Class and Object
Experiment No.3: Class and Object
Rollno : 19EE1135
Batch: A2
5. Theory: 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.
• A Class is a user defined data-type which has data members and member functions.
• Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions
defines the properties and behavior of the objects in a Class.
• In the above example of class Car, the data member will be speed limit, mileage etc
and member functions can be apply brakes, increase speed etc.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it
is instantiated (i.e. an object is created) memory is allocated.
Syntax:
A class is defined in C++ using keyword class followed by the name of class. The body of class
is defined inside the curly brackets and terminated by a semicolon at the end.
Class ClassName
{ Acess specifier: // can be private,public or protected
Data members; // variable to be used
Member Function(){} //Method to acess data members
}; // class name ends with a semicolon
In this program, the object of class Student is used to access the methods getStudentData()
and DisplayStudentData() of this class.
Name: Ashutosh Dhande
Rollno : 19EE1135
Batch: A2
6. Algorithm 1. Start.
2. Declare a class named Student.
3. Initialize the required variables.
4. Declare a method getStudentData() and get value of Rollno,Name ,Class, Division and
Marks from the user.
5. Declare a method DisplayStudentData() to displaying the student details on the screen
6. Declare main() and create object of class Students. And access the methods with the help
of object.
7. Display the details.
8. Stop.
Program:
#include <iostream>
class student
{ char student_name[30];
char roll_no[10];
char branch[20];
int year;
char divi[5];
public:
void getdetails();
void putdetails();
};
void student::getdetails(void)
{ cout<<"\nEnter the name: ";
cin >>student_name ;
cout<<"\nEnter the rollno: ";
Name: Ashutosh Dhande
Rollno : 19EE1135
Batch: A2
cin>>roll_no;
cout<<"\n Enter the branch name:";
cin>>branch;
cout<<"\nEnter division: ";
cin>>divi;
cout<<"\n Enter the year:";
cin >>year ;
}
void student::putdetails(void) {
cout<<"\nSTUDENT DETAILS........";
cout<<"\nNAME:"<<student_name;
cout<<"\nROLL NO.:"<<roll_no;
cout<<"\nBRANCH NAME:"<<branch;
cout<<"\nDIVISION:"<<divi;
cout<<"\nYEAR:"<<year;
}
int main()
{ student std;
std.getdetails();
std.putdetails();
return 0;
}
Output:
Name: Ashutosh Dhande
Rollno : 19EE1135
Batch: A2
7. Conclusion:
After performing this experiment we are able to create a class and object. The classes are
interacting with each other by accessing the methods and members of class using different
objects.