// Program to illustrate the working of
// objects and class in C++ Programming
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculate_area() {
return length * breadth;
}
double calculate_volume() {
return length * breadth * height;
}
};
int main() {
// create object of Room class
Room room1;
// 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;
}
/* Read and Print Student Information Class Example Program In C++ */
/* Class C++ Programs, Understanding Class,C++ Examples */
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
// Student Class Declaration
class StudentClass {
private://Access - Specifier
//Member Variable Declaration
char name[20];
int regNo, sub1, sub2, sub3;
float total, avg;
public://Access - Specifier
//Member Functions read() and print() Declaration
void read() {
//Get Input Values For Object Variables
cout << "Enter Name :";
cin >> name;
cout << "Enter Registration Number :";
cin >> regNo;
cout << "Enter Marks for Subject 1,2 and 3 :";
cin >> sub1 >> sub2>> sub3;
}
void sum() {
total = sub1 + sub2 + sub3;
avg = total / 3;
}
void print() {
//Show the Output
cout << "Name :" << name << endl;
cout << "Registration Number :" << regNo << endl;
cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;
cout << "Total :" << total << endl;
cout << "Average :" << avg << endl;
}
};
int main() {
// Object Creation For Class
StudentClass stu1, stu2;
cout << "Read and Print Student Information Class Example Program In C++\n";
cout << "\nStudentClass : Student 1" << endl;
stu1.read();
stu1.sum();
stu1.print();
cout << "\nStudentClass : Student 2" << endl;
stu2.read();
stu2.sum();
stu2.print();
getch();
return 0;
}
/* Program For Simple Class Example Program For Prime Number In C++ */
#include<iostream>
#include<conio.h>
using namespace std;
// Class Declaration
class prime {
//Member Variable Declaration
int a, k, i;
public:
prime(int x) {
a = x;
}
// Object Creation For Class
void calculate() {
k = 1;
{
for (i = 2; i <= a / 2; i++)
if (a % i == 0) {
k = 0;
break;
} else {
k = 1;
}
}
}
void show() {
if (k == 1)
cout << "\n" << a << " is Prime Number.";
else
cout << "\n" << a << " is Not Prime Numbers.";
}
};
//Main Function
int main() {
int a;
cout << "Enter the Number:";
cin>>a;
// Object Creation For Class
prime obj(a);
// Call Member Functions
obj.calculate();
obj.show();
getch();
return 0;
}