0% found this document useful (0 votes)
45 views20 pages

Assignment of C++ by Gourob CSIT 212061001

The document contains 15 programming assignments involving the use of classes and constructors in C++. The assignments cover topics such as creating constructors that take input parameters, calculating areas and volumes using constructors, using copy constructors, friend functions, and inline functions. The programs demonstrate how to define classes, declare objects, write constructors and member functions, take user input, perform calculations, and display output.

Uploaded by

Bedoor
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)
45 views20 pages

Assignment of C++ by Gourob CSIT 212061001

The document contains 15 programming assignments involving the use of classes and constructors in C++. The assignments cover topics such as creating constructors that take input parameters, calculating areas and volumes using constructors, using copy constructors, friend functions, and inline functions. The programs demonstrate how to define classes, declare objects, write constructors and member functions, take user input, perform calculations, and display output.

Uploaded by

Bedoor
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/ 20

Assignment- 1

1. Write a program by creating a constructor which takes one input parameters.

#include <bits/stdc++.h>

using namespace std;

class userValidation

float validationKey;

public:

userValidation()

cout << "Input a digit: ";

cin >> validationKey;

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>

using namespace std;

class demo

float key1;

int key2;

public:

demo()

float param1;

int param2;

cout << "Input two digits: ";

cin >> param1 >> 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>

using namespace std;

class AreaOfRectangle

float area, length, width;

public:

AreaOfRectangle()

cout << "Enter length and width for the rectangle: ";

cin >> length >> width;

area = length * width;

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

another function to show result.

#include<iostream>

using namespace std;

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)

cout<< "\nThe Number is prime Number.\n";

else

cout<<"\nThe Number is Not prime.\n"; }};

int main(){

int a;

cout<<"\nEnter any Number: ";

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>

using namespace std;

class volumeOfCube

int A;

public:

volumeOfCube()

cout << "Enter edge, A: ";

cin >> this->A;

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>

using namespace std;

class employeePersonDetails

public:

string name;

int age;

bool isGraduate;

float height;

employeePersonDetails(string s, int a, bool i, float h)

name = s;

age = a;

isGraduate = i;

height = h;

void display()

cout << "Name: " << name << endl

<< "age: " << age << endl

<< "height: " << height << endl

<< "Graduated: " << (isGraduate ? "Yes" : "No") << endl;}};

int main(){

employeePersonDetails person1"Sadequr Rahman", 23,

false, 5.4); person1.display();

cout << "------------------------\n"; employeePersonDetails

person2("Jahid", 23, false, 5.6); person2.display();

return 0;}
7. Write a program to find factorial using copy constructor.

#include <iostream>

using namespace std;

class fact

{int n, i,

facti; public:

fact(int x){

n = x;

facti = 1;}

fact(fact &x){

n = x.n;

facti = 1;}

void calculate(){

for (i = 1; i <= n; i++){

facti = facti * i;}}

void display(){

cout << "Factorial : " << facti << endl;}};

int main(){

int x;

cout << "Enter Value : ";

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>

using namespace std;

class temp

int a, b,

add; public:

void input()

cout << "Enter the value of a and b: ";

cin >> a >> b;

friend void add(temp &t);

void display()

cout << "The sum is: " << add << endl;

};

void add(temp &t)

t.add = t.a + t.b;

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>

using namespace std;

class AreaOfBox

float B, L, A, Area;

friend void

calculateArea(AreaOfBox); };

void calculateArea(AreaOfBox box)

int n;

//! Error handling to check if the box is Square or

cout << "Is the box\n1. Square\n2. Rectangle"

<< endl; cout << "Enter your choice: "; cin >> n;

if (n == 1)

cout << "Enter Length = ";

cin >> box.A;

box.Area = box.A * box.A;

cout << "Area of the square box is: " << box.Area << " unit" << endl;

else if (n == 2)

cout << "Enter Length = ";

cin >> box.L;

cout << "Enter Breadth = ";

cin >> box.B;

box.Area = box.L * box.B;

cout << "Area of the Rectangle box: " << box.Area << " unit" << endl;

}
else

cout << "Invalid input\n";

int main()

AreaOfBox a;

calculateArea(a);

return 0;

}
10. C++ program to reverse a Number using class.

#include<iostream>

using namespace std;

class rev

private:

int n,n1,rn=0,d;

public:

void input();

void calc();

void display();

};

void rev::input()

cout<<"\nEnter any positive no. :: ";

cin>>n;

void rev::calc()

n1=n;

while(n>0)

d=n%10;

rn=(rn*10)+d;

n/=10;

void rev::display()

cout<<"\nReverse of [ "<<n1<<" ] is :: "<<rn<<"\n";


}

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>

using namespace std;

class student

private:

char name[30];

int ID;

string department;

string semester;

public:

void getDetails(void);

void putDetails(void);

};

void student::getDetails(void)

cout << "Enter name: ";

cin >> name;

cout << "Enter ID: ";

cin >> ID;

cout << "Enter Department: ";

cin >> department;

cout << "Enter semester: ";

cin >> semester;

void student::putDetails(void)

cout << "Student details:\n";


cout << "----------------------\n";

cout << "Name: " << name << ", ID: " << ID << ", Semester: " <<

semester << ", Department: " <<

department << endl;

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

based on salary, showEmployee( ) to display employee details.

#include <bits/stdc++.h>

using namespace std;

class employee

int emp_number;

string emp_name;

string emp_da;

float emp_net_sal;

public:

void get_emp_details();

float find_net_salary(float basic, float da, float it);

void show_emp_details();

};

void employee :: get_emp_details()

cout << "Enter employee ID: ";

cin >> emp_number;

cout << "Enter employee name: ";

cin >> emp_name;

cout << "Enter employee Designation:";

cin >> emp_da;

cout << "Enter employee Salary: ";

cin >> emp_net_sal;

float employee ::find_net_salary(float basic, float da, float it)

return (basic + da) - it;


}

void employee ::show_emp_details()

cout << "\n\n**** Details of Employee ****";

cout << "\nEmployee ID : " << emp_name;

cout << "\nEmployee number : " << emp_number;

cout << "\nSalary : " << emp_net_sal;

cout << "\nEmployee Designation : " << emp_da;

cout << "\n-------------------------------\n\n";

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>

using namespace std;

#define PI 3.1416

/*

! Calculation

* Circle radius = r

* Circle Area = PI.r^2

* Circle Circumference, C = 2 * PI *

r */

class CircleCalculation

float radius;

public:

void get_rad(float r)

radius = r;

void circle_area()

float area = PI * pow(radius, 4);

cout << "Area of cirlce is: " << area << endl;

void circle_circumference()

float circumference = 4 * PI * radius;

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>

using namespace std;

class SquareOfNumber

float num;

public:

inline float square_calculation(float n)

num = n;

return pow(num, 2);

};

int main()

float n;

cout << "Enter number to get it's square: ";

cin >> n;

SquareOfNumber s;

cout << "Reuslt: ";

cout << s.square_calculation(n) << endl;

return 0;

}
15. Program to find cube of a number using inline function

#include <bits/stdc++.h>

using namespace std;

class CubeOfNumber

float num;

public:

inline float cube_calculation(float n)

num = n;

return pow(num, 3);

};

int main()

float n;

cout << "Enter number to get it's cube: ";

cin >> n;

CubeOfNumber c;

cout << "Reuslt: ";

cout << c.cube_calculation(n) << endl;

return 0;

You might also like