Oopm Ex-2
Oopm Ex-2
Problem Statement: Create a class named student. Take name, Enrollment number and contact
number as Data members and print their values. Create Objects Obj1, Obj2, Obj3 and set data
members values using these objects also print their values
Outcomes: Students will be able to learn how to create a simple class and objects
Theory:
Classes and objects are the two main aspects of object-oriented programming. Object is termed
as an instance of a class, and it has its own state, behaviour and identity.
For example: Class of birds, all birds can fly and they all have wings and beaks. So here flying
is a behaviour and wings and beaks are part of their characteristics. And there are many different
birds in this class with different names but they all possess this behaviour and characteristics.
Similarly, class is just a blue print, which declares and defines characteristics and behavior,
namely data members and member functions respectively. And all objects of this class will share
these characteristics and behaviour.
Example2:
Class: Car
Objects: Volvo, Audi, Toyota
1 | Page
We can access the data members and member functions of a class by using a .(dot) operator
Program:
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
string enrolment;
string contactNo;
void print_details() {
cout << "Name: " << name << endl;
cout << "Enrolment Number: " << enrolment << endl;
cout << "Contact Number: " << contactNo << endl;
cout << endl;
}
};
int main() {
Student obj1, obj2, obj3;
obj1.name = "Dheeraj";
obj1.enrollmentNo = "0818CS23100";
obj1.contactNo = "1234567890";
obj2.name = "Gautam";
obj2.enrollmentNo = "0818CS23101";
obj2.contactNo = "9876543210";
obj3.name = "Bhawesh";
obj3.enrollmentNo= "0818CS23102";
obj3.contactNo = "5894732505";
obj1.print_details();
obj2.print_details();
obj3.print_details();
2 | Page
return 0;
}
Output:
Name: Dheeraj
Enrolment Number: 0818CS23100
Contact Number: 1234567890
Name: Gautam
Enrolment Number: 0818CS23101
Contact Number: 9876543210
Name: Bhawesh
Enrolment Number: 0818CS23102
Contact Number: 5894732505
3 | Page
2. Write a syntax to call data member of a class?
4 | Page