OOPs Programs
OOPs Programs
// 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;
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;
return 0;
}