Problem 1
#include <iostream>
using namespace std;
class student {
public:
string name;
int roll_no;
};
int main() {
student st1;
st1.name = "john";
st1.roll_no = 2;
cout << "Name: " << st1.name << endl;
cout << "roll: " << st1.roll_no << endl;
return 0;
Problem 2
#include <iostream>
using namespace std;
class Student{
public:
string roll_no,phonenumber;
string name,address;
void printDetails(){
cout<<"Name: "<<name<<endl;
cout<<"Address: "<<address<<endl;
cout<<"Roll no: "<<roll_no<<endl;
cout<<"Phone number: "<<phonenumber<<endl;
};
int main(){
Student student1,student2;
student1.name = "Sam";
student1.address = "kuril";
student1.roll_no = "19";
student1.phonenumber = "0123456789";
student1.printDetails();
cout<<endl;
student2.name = "John";
student2.address = "Nikunjo";
student2.roll_no = "11";
student2.phonenumber = "0123456789";
student2.printDetails();
return 0;
}
Problem 3
#include <iostream>
#include<cmath>
using namespace std;
class Triangle {
public:
double side1, side2, side3;
double area(){
double s = (side1 + side2 + side3) / 2.0;
double area = sqrt(s*(s - side1)*(s - side2)*(s - side3));
return area;
double perimeter() {
return side1 + side2 + side3;
void display(){
cout << "Area: " << area()<<endl;
cout << "Perimeter: "<< perimeter()<<endl;
};
int main() {
Triangle triangle;
triangle.side1 = 3;
triangle.side2 = 4;
triangle.side3 = 5;
triangle.display();
return 0;
Problem 4
#include <iostream>
using namespace std;
class Triangle
public:
int s1, s2, s3;
Triangle(int a, int b, int c)
s1 = a;
s2 = b;
s3 = c;
void print_area()
double s = (s1+s2+s3)/2;
cout << "Area: "<<s<< endl;
cout << "Perimeter is " << (s1+s2+s3) << endl;
};
int main()
Triangle t(3,4,5);
t.print_area();
return 0;
}
Problem 5
#include <iostream>
using namespace std;
class Rectangle {
public:
int length;
int breadth;
Rectangle(int l, int b) {
length = l;
breadth = b;
int Area() {
return length * breadth;
};
int main() {
Rectangle r1(4, 5);
Rectangle r2(5, 8);
cout << "Area of Rectangle 1: " << r1.Area() << endl;
cout << "Area of Rectangle 2: " << r2.Area() << endl;
return 0;
Problem 6
#include <iostream>
using namespace std;
class Area{
public:
int length;
int breadth;
void setDim(int l, int b) {
length = l;
breadth = b;
int getArea(){
return length*breadth;
};
int main(){
Area a;
a.setDim(4,5);
cout << a.getArea() << endl;
return 0;
Problem 7
#include <iostream>
using namespace std;
class Area {
public:
int length;
int breadth;
Area(int l, int b) {
length = l;
breadth = b;
int returnArea() {
return length * breadth;
};
int main() {
int l, b;
cout << "Enter length of rectangle: ";
cin >> l;
cout << "Enter breadth of rectangle: ";
cin >> b;
Area a(l, b);
cout << "Area of rectangle: " << a.returnArea();
return 0;
Problem 8
#include <iostream>
using namespace std;
class Average {
public:
void calculateAverage(int num1, int num2, int num3){
double average = (double)(num1 + num2 + num3) / 3.0;
cout << "The average of the three numbers is: " << average << std::endl;
};
int main()
int num1, num2, num3;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Enter the third number: ";
cin >> num3;
Average().calculateAverage(num1, num2, num3);
return 0;
}
Problem 9
#include <iostream>
using namespace std;
class Complex {
public:
float real;
float imag;
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
Complex operator * (Complex const &obj) {
Complex res;
res.real = real * obj.real - imag * obj.imag;
res.imag = real * obj.imag + imag * obj.real;
return res;
}
};
int main() {
Complex c1, c2;
cout << "Enter real and imaginary parts of first complex number: ";
cin >> c1.real >> c1.imag;
cout << "Enter real and imaginary parts of second complex number: ";
cin >> c2.real >> c2.imag;
Complex sum = c1 + c2;
Complex diff = c1 - c2;
Complex prod = c1 * c2;
cout << "Sum = " << sum.real << " + " << sum.imag << "i" << endl;
cout << "Difference = " << diff.real << " + " << diff.imag << "i" << endl;
cout << "Product = " << prod.real << " + " << prod.imag << "i" << endl;
return 0;
Problem 10
#include <iostream>
using namespace std;
class Volume{
private:
int length;
int width;
int height;
public:
void set(int l, int w, int h){
length=l;
width=w;
height= h;
int getvol(){
return length*width*height;
};
int main(){
Volume rect;
int length , width,height;
cout<<"Enter the length:";
cin>>length;
cout<<"Enter the width: ";
cin>>width;
cout<<"Enter the height: ";
cin>>height;
rect.set(length, width, height);
int volume = rect.getvol();
cout<<"The Volume is: "<<volume;
return 0;