Assignment of C++ by Gourob CSIT 212061001
Assignment of C++ by Gourob CSIT 212061001
#include <bits/stdc++.h>
class userValidation
float validationKey;
public:
userValidation()
cout << "Your input is: " << validationKey << endl;
};
int main()
userValidation user1;
return 0;
}
2. Write a program by creating a constructor which takes two input parameters.
#include <bits/stdc++.h>
class demo
float key1;
int key2;
public:
demo()
float param1;
int param2;
key1 = param1;
key2 = param2;
cout << "Your inputs are: " << key1 << " and " << key2 << endl;
};
int main()
demo obj1;
return 0;
}
3. C++ Program to find Area of Rectangle using constructor.
#include <bits/stdc++.h>
class AreaOfRectangle
public:
AreaOfRectangle()
cout << "Enter length and width for the rectangle: ";
cout << "Area of Rectangle as per your input is: " << area << endl;
};
int main()
AreaOfRectangle obj;
return 0;
}
4. Calculate prime number creating class. Use constructor, a function for calculation and
#include<iostream>
class prime
{int a,k,i;
public:
prime(int x)
{a=x; }
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)
else
int main(){
int a;
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
return 0; }
5. Find the volume of a Cube by implicitly passing parameter to the constructor.
#include <bits/stdc++.h>
class volumeOfCube
int A;
public:
volumeOfCube()
cout << "A is: " << A << " Volume is: " << pow(A, 3) << endl;
};
int main()
volumeOfCube obj1;
return 0;
}
6. Display a person detail by explicitly passing parameter to the constructor.
#include <bits/stdc++.h>
class employeePersonDetails
public:
string name;
int age;
bool isGraduate;
float height;
name = s;
age = a;
isGraduate = i;
height = h;
void display()
int main(){
return 0;}
7. Write a program to find factorial using copy constructor.
#include <iostream>
class fact
{int n, i,
facti; public:
fact(int x){
n = x;
facti = 1;}
fact(fact &x){
n = x.n;
facti = 1;}
void calculate(){
void display(){
int main(){
int x;
cin >> x;
fact f1(x);
f1.calculate();
f1.display();
fact f2(f1);
f2.calculate();
f2.display();
return 0;
}
8. Add two members of two different classes using the friend function.
#include <iostream>
class temp
int a, b,
add; public:
void input()
void display()
cout << "The sum is: " << add << endl;
};
int main()
temp t1;
t1.input();
add(t1);
t1.display();
return 0;
}
9. Write an OOP program to calculate the area of a box using the friend function.
#include <bits/stdc++.h>
class AreaOfBox
float B, L, A, Area;
friend void
calculateArea(AreaOfBox); };
int n;
<< endl; cout << "Enter your choice: "; cin >> n;
if (n == 1)
cout << "Area of the square box is: " << box.Area << " unit" << endl;
else if (n == 2)
cout << "Area of the Rectangle box: " << box.Area << " unit" << endl;
}
else
int main()
AreaOfBox a;
calculateArea(a);
return 0;
}
10. C++ program to reverse a Number using class.
#include<iostream>
class rev
private:
int n,n1,rn=0,d;
public:
void input();
void calc();
void display();
};
void rev::input()
cin>>n;
void rev::calc()
n1=n;
while(n>0)
d=n%10;
rn=(rn*10)+d;
n/=10;
void rev::display()
int main ()
rev r;
r.input();
r.calc();
r.display();
return 0;
}
11. Create a class Student with data members ID, name, department and semester. Write methods
getinfo()-to take user input, putinfo( ) to display student details. Define both function outside
of the class.
#include <bits/stdc++.h>
class student
private:
char name[30];
int ID;
string department;
string semester;
public:
void getDetails(void);
void putDetails(void);
};
void student::getDetails(void)
void student::putDetails(void)
cout << "Name: " << name << ", ID: " << ID << ", Semester: " <<
int main()
student std;
std.getDetails();
std.putDetails();
return 0;
}
12. Create a class Employee with data members - employeeID, name, designation and salary.
Write methods getEmployee( )-to take user input, display( )-to display grade of employees
#include <bits/stdc++.h>
class employee
int emp_number;
string emp_name;
string emp_da;
float emp_net_sal;
public:
void get_emp_details();
void show_emp_details();
};
int main()
employee emp;
emp.get_emp_details();
emp.show_emp_details();
return 0;
}
13. Write a program which uses get_rad() function to accepts the radius of a circle from the user
and compute the area and circumference by using 2 other separate functions.
#include <bits/stdc++.h>
#define PI 3.1416
/*
! Calculation
* Circle radius = r
* Circle Circumference, C = 2 * PI *
r */
class CircleCalculation
float radius;
public:
void get_rad(float r)
radius = r;
void circle_area()
cout << "Area of cirlce is: " << area << endl;
void circle_circumference()
cout << "Circumference of circle is: " << circumference << endl;
};
int main()
CircleCalculation c;
c.get_rad(20);
c.circle_area();
c.circle_circumference();
return 0;
}
14. Find the square of any number using inline function.
#include <bits/stdc++.h>
class SquareOfNumber
float num;
public:
num = n;
};
int main()
float n;
cin >> n;
SquareOfNumber s;
return 0;
}
15. Program to find cube of a number using inline function
#include <bits/stdc++.h>
class CubeOfNumber
float num;
public:
num = n;
};
int main()
float n;
cin >> n;
CubeOfNumber c;
return 0;