0% found this document useful (0 votes)
14 views3 pages

OOPs Programs

OBJECT ORINTED PROGRAMS IN c++
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)
14 views3 pages

OOPs Programs

OBJECT ORINTED PROGRAMS IN c++
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/ 3

Class and Object Illustration by Examples

// Write a program to declare an integer variable in a class, assign value to it in the main function and display it.

#include<iostream>
using namespace std;
class intro
{
public:
int a;
};
int main()
{
intro obj;
obj.a=10;
cout<<obj.a;
return 0;

// Write a program to declare a string variable in a class, assign value to it in the main function and display it.

#include<iostream>
using namespace std;
class intro
{
public: //Access Specifier
string a; // var declaration
};
int main()
{
intro obj;
obj.a="Hello World";
cout<<obj.a;
return 0;
}
// Write a program to display hello world by using member function of a class
#include<iostream>
using namespace std;
class intro
{
public: //Access Specifier
void show()
{
cout<<"hello world";

}
};
int main()
{
intro obj;
obj.show();
return 0;
}

// Program to find the area and volume of the room by using class and object

#include <iostream>
using namespace std;

class Room // create a class


{

public:
double length;
double breadth;
double height;

double calculate_area()
{
return length * breadth;
}

double calculate_volume()
{
return length * breadth * height;
}
};

int main()
{
Room room1; // create object of Room class
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculate_area() << endl;
cout << "Volume of Room = " << room1.calculate_volume() << endl;

return 0;
}

You might also like