0% found this document useful (0 votes)
7 views4 pages

Oopm Ex-2

Uploaded by

amnikshukla2501
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Oopm Ex-2

Uploaded by

amnikshukla2501
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EXPERIMENT NO.

Aim / Title: To understand the basic concept of OOPM

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

Objectives: Creating a Class and objects in C++

Outcomes: Students will be able to learn how to create a simple class and objects

Pre-requisite: Basic knowledge of C++

Hardware requirements: Processor (CPU) with 2 gigahertz (GHz) frequency or above


A minimum of 2 GB of RAM
Monitor Resolution 1024 X 768 or higher
A minimum of 20 GB of available space on the hard disk

Software requirements: Any C++ Compiler/C++ IDE

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.

More Examples: Example1:


Class: Fruit
Objects: Apple, Pineapple, Mango

Example2:
Class: Car
Objects: Volvo, Audi, Toyota

Syntax to define objects in C++


Class_Name Object_Var_Name;

1 | Page
We can access the data members and member functions of a class by using a .(dot) operator

Instructions: Write program in C++ language

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

Sample Viva Questions and Answers:

1. Write Example of a class and objects other than mentioned examples!

3 | Page
2. Write a syntax to call data member of a class?

3. Write a syntax to call member function of a class.

4. Give an example of a class from real world.

5. What is the state, behaviour and property of an object.

6. What is the drawback of this program.

Roll Name of Date of Date of Grade Sign of Sign of


No. Student Performance Evaluation Student Faculty

4 | Page

You might also like