c++
c++
Name-Mohammed Qadir
Roll no.-204735
Q1) Create a Matrix class. Write a menu-driven program to perform following Matrix
operations:
a. Sum
b. Transpose
Ans:- #include <iostream>
using namespace std;
class Matrix {
private:
int rows, cols;
int** data;
public:
Matrix(int r, int c) : rows(r), cols(c) {
data = new int*[rows];
for (int i = 0; i < rows; ++i) {
data[i] = new int[cols]; }}
void input() {
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j]; }}}
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";}
cout << endl; }}
Matrix operator+(const Matrix& other) const {
Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] + other.data[i][j]; }}
return result; }
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[j][i] = data[i][j];} }
return result; }
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i];}
delete[] data; }};
int main() {
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
Matrix mat1(rows, cols);
cout << "Matrix 1:" << endl;
mat1.input();
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Sum of two matrices\n";
cout << "2. Transpose of the matrix\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Matrix mat2(rows, cols);
cout << "Matrix 2:" << endl;
mat2.input();
Matrix sum = mat1 + mat2;
cout << "Sum of matrices:" << endl;
sum.display();
break;}
case 2: {
Matrix transposed = mat1.transpose();
cout << "Transpose of the matrix:" << endl;
transposed.display();
break;}
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl; }
} while (choice != 3);
return 0;}
Q2: Create a Matrix class. Write a menu-driven program to perform following
Matrix operations:
a. Product
b. Transpose
Ans:- #include <iostream>
using namespace std;
class Matrix {
private:
int rows, cols;
int** data;
public:
Matrix(int r, int c) : rows(r), cols(c) {
data = new int*[rows];
for (int i = 0; i < rows; ++i) {
data[i] = new int[cols]; }}
void input() {
cout << "Enter elements of the matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> data[i][j]; }}}
void display() const {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << data[i][j] << " ";}
cout << endl; }}
Matrix operator*(const Matrix& other) const {
if (cols != other.rows) {
throw invalid_argument("Matrix dimensions do not match for
multiplication."); }
Matrix result(rows, other.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.cols; ++j) {
result.data[i][j] = 0;
for (int k = 0; k < cols; ++k) {
result.data[i][j] += data[i][k] * other.data[k][j]; }}}
return result; }
Matrix transpose() const {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[j][i] = data[i][j]; }}
return result; }
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i]; }
delete[] data; }};
int main() {
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
Matrix mat1(rows, cols);
cout << "Matrix 1:" << endl;
mat1.input();
int choice;
do {
cout << "\nMenu:\n";
cout << "1. Product of two matrices\n";
cout << "2. Transpose of the matrix\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
Matrix mat2(rows, cols);
cout << "Matrix 2:" << endl;
mat2.input();
try {
Matrix product = mat1 * mat2;
cout << "Product of matrices:" << endl;
product.display();
} catch (const invalid_argument& e) {
cout << e.what() << endl;
}
break;
}
case 2: {
Matrix transposed = mat1.transpose();
cout << "Transpose of the matrix:" << endl;
transposed.display();
break;
}
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl; }
} while (choice != 3);
return 0;}
Q3: Define a class Person having name as a data member. Inherit two classes
Student and Employee from Person. Student has additional attributes as
course, marks and year and Employee has department and salary. Write
display() method in all the three classes to display the corresponding
attributes.
Ans:- #include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
public:
Person(string n) : name(n) {}
virtual void display() const {
cout << "Name: " << name << endl; }};
class Student : public Person {
private:
string course;
int marks;
int year;
public:
Student(string n, string c, int m, int y) : Person(n), course(c), marks(m), year(y) {}
void display() const override {
Person::display();
cout << "Course: " << course << endl;
cout << "Marks: " << marks << endl;
cout << "Year: " << year << endl; }};
class Employee : public Person {
private:
string department;
float salary;
public:
Employee(string n, string d, float s) : Person(n), department(d), salary(s) {}
void display() const override {
Person::display();
cout << "Department: " << department << endl;
cout << "Salary: " << salary << endl; }};
int main() {
Student student("Alice", "Computer Science", 90, 2024);
Employee employee("Bob", "HR", 50000);
cout << "Student Details:" << endl;
student.display();
cout << "\nEmployee Details:" << endl;
employee.display();
return 0;}
Q4: Create a Triangle class. Add exception handling statements to ensure the
following conditions: all sides are greater than 0 and sum of any two sides is
greater than the third side.
Ans:- #include <iostream>
#include <stdexcept>
using namespace std;
class Triangle {
private:
double side1, side2, side3;
public:
Triangle(double s1, double s2, double s3) {
if (s1 <= 0 || s2 <= 0 || s3 <= 0) {
throw invalid_argument("All sides must be greater than 0."); }
if (s1 + s2 <= s3 || s1 + s3 <= s2 || s2 + s3 <= s1) {
throw invalid_argument("The sum of any two sides must be greater than
the third side."); }
side1 = s1;
side2 = s2;
side3 = s3; }
void display() const {
cout << "Triangle sides: " << side1 << ", " << side2 << ", " << side3 << endl;}};
int main() {
try {
double s1, s2, s3;
cout << "Enter the sides of the triangle: ";
cin >> s1 >> s2 >> s3;
Triangle triangle(s1, s2, s3);
triangle.display();
} catch (const invalid_argument& e) {
cout << "Error: " << e.what() << endl; }
return 0;}
Q5: Create a class Student containing fields for Roll No., Name, Class, Year and
Total Marks. Write a program to store 5 objects of Student class in a file.
Retrieve these records from the file and display them.
Ans:- #include <iostream>
#include <fstream>
using namespace std;
class Student {
private:
int rollNo;
string name;
string studentClass;
int year;
int totalMarks;
public:
Student() {}
Student(int r, string n, string c, int y, int m) : rollNo(r), name(n),
studentClass(c), year(y), totalMarks(m) {}
void input() {
cout << "Enter Roll No.: ";
cin >> rollNo;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Class: ";
getline(cin, studentClass);
cout << "Enter Year: ";
cin >> year;
cout << "Enter Total Marks: ";
cin >> totalMarks; }
void display() const {
cout << "Roll No.: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Class: " << studentClass << endl;
cout << "Year: " << year << endl;
cout << "Total Marks: " << totalMarks << endl; }
friend ofstream& operator<<(ofstream& ofs, const Student& s);
friend ifstream& operator>>(ifstream& ifs, Student& s); };
ofstream& operator<<(ofstream& ofs, const Student& s) {
ofs << s.rollNo << endl;
ofs << s.name << endl;
ofs << s.studentClass << endl;
ofs << s.year << endl;
ofs << s.totalMarks << endl;
return ofs; }
ifstream& operator>>(ifstream& ifs, Student& s) {
ifs >> s.rollNo;
ifs.ignore();
getline(ifs, s.name);
getline(ifs, s.studentClass);
ifs >> s.year;
ifs >> s.totalMarks;
return ifs; }
int main() {
Student students[5];
ofstream outFile("students.txt");
cout << "Enter details of 5 students:" << endl;
for (int i = 0; i < 5; ++i) {
students[i].input();
outFile << students[i]; }
outFile.close();
ifstream inFile("students.txt");
Student s;
cout << "\nDetails of students from file:" << endl;
while (inFile >> s) {
s.display();
cout << endl; }
inFile.close();
return 0; }
Q6: Create a class Vehicle having mileage (int data type) and price (float data
type) as public data members.
(i) Create a sub class Car which is publicly inherited from Vehicle class having
fuel type(char data type) and warranty (int data type) as public data
members.
(ii) Derive another subclass Audi which is publicly inherited from class Car
having data member model type (char data type) and member function
getdata() which takes the input from the user and putdata() to display all
the information of Audi car.
(iii) Define main() function to print all information (mileage, price, fuel type,
warranty, model type) about an object of class Audi.
Ans:- #include <iostream>
using namespace std;
class Vehicle {
public:
int mileage;
float price;
Vehicle(int m, float p) : mileage(m), price(p) {}};
class Car : public Vehicle {
public:
char fuelType;
int warranty;
Car(int m, float p, char f, int w) : Vehicle(m, p), fuelType(f), warranty(w) {}};
class Audi : public Car {
public:
char modelType;
Audi(int m, float p, char f, int w, char mt) : Car(m, p, f, w), modelType(mt) {}
void getdata() {
cout << "Enter mileage: ";
cin >> mileage;
cout << "Enter price: ";
cin >> price;
cout << "Enter fuel type (P for Petrol, D for Diesel): ";
cin >> fuelType;
cout << "Enter warranty (in years): ";
cin >> warranty;
cout << "Enter model type: ";
cin >> modelType; }
void putdata() const {
cout << "Mileage: " << mileage << endl;
cout << "Price: " << price << endl;
cout << "Fuel Type: " << fuelType << endl;
cout << "Warranty: " << warranty << endl;
cout << "Model Type: " << modelType << endl; }};
int main() {
Audi myAudi(0, 0.0, ' ', 0, ' ');
myAudi.getdata();
cout << "\nAudi Car Details:" << endl;
myAudi.putdata();
return 0; }
Q7: Write a program to create a class Complex which has two data members: real
part a
(integer) and imaginary part b (float). Write the following member functions for
this class:
(i) A default constructor to initialize a and b to 0.
(ii) A parameterized constructor to initialize a and b to passed values.
(iii) A function print() to display the complex number in the form a + ib i.e. for a = 4
and b = 5 the output of print should be 4 +i5.
Ans:- #include <iostream>
using namespace std;
class Complex {
private:
int a;
float b;
public:
Complex() : a(0), b(0.0) {}
Complex(int real, float imag) : a(real), b(imag) {}
void print() const {
cout << a << " +i" << b << endl; }};
int main() {
Complex c1;
Complex c2(4, 5.0);
c1.print();
c2.print();
return 0; }
Q9: Write a C++ program that defines a class Item with the following
specifications:
Private Data Members: code (depicting item unique number), price (price of the
item).Public Member Functions: getData(): A function defined outside the class
that assigns valuesto the code and price variables, displayData(): A function to
display the values of code and price on the console.
Ans:- #include <iostream>
using namespace std;
class Item {
private:
int code;
float price;
public:
void getData();
void displayData() const; };
void Item::getData() {
cout << "Enter code: ";
cin >> code;
cout << "Enter price: ";
cin >> price;
}
void Item::displayData() const {
cout << "Code: " << code << endl;
cout << "Price: " << price << endl;
}
int main() {
Item item;
item.getData();
item.displayData();
return 0;
}
Q10: Define a class Circle with a member function to calculate the circumference
of a circle.
Use the formula circumference = 2 * 3.14 * radius. The radius should be a private
member variable.
Ans:- #include <iostream>
using namespace std;
class Circle {
private:
float radius;
public:
void setRadius(float r) {
radius = r; }
float circumference() const {
return 2 * 3.14 * radius; }};
int main() {
Circle c;
float r;
cout << "Enter radius: ";
cin >> r;
c.setRadius(r);
cout << "Circumference: " << c.circumference() << endl;
return 0;
}
Q11: Create a class Rectangle with private members length and width. Write a
member function to calculate and display the area of the rectangle.
Ans:- #include <iostream>
using namespace std;
class Rectangle {
private:
float length;
float width;
public:
void setDimensions(float l, float w) {
length = l;
width = w; }
float area() const {
return length * width; }
void displayArea() const {
cout << "Area: " << area() << endl; }};
int main() {
Rectangle rect;
float l, w;
cout << "Enter length and width: ";
cin >> l >> w;
rect.setDimensions(l, w);
rect.displayArea();
return 0;
}
Q12: Write a simple program using a function template to swap two variables of
any data type.
Ans:- #include <iostream>
using namespace std;
template <typename T>
void swapVariables(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swapVariables(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
cout << "Before swap: a = " << a << ", b = " << b << endl;
swapVariables(a, b);
cout << "After swap: a = " << a << ", b = " << b << endl;
return 0;}
}
Q13: Define a function template to calculate the maximum of two numbers
(integers or floats).
Ans:- #include <iostream>
using namespace std;
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b; }
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
cout << "Maximum of " << x << " and " << y << " is " << maximum(x, y) << endl;
cout << "Maximum of " << a << " and " << b << " is " << maximum(a, b) << endl;
return 0;
}
Q14: Create a template function to calculate the square of a number. Test the
function with different data types.
Ans:- #include <iostream>
using namespace std;
template <typename T>
T square(T x) {
return x * x; }
int main() {
int x = 5;
float y = 5.5;
cout << "Square of " << x << " is " << square(x) << endl;
cout << "Square of " << y << " is " << square(y) << endl;
return 0;
}
Q15: Write a program that demonstrates how a single function template can be
used to perform addition on integers, floats, and doubles.
Ans:- #include <iostream>
using namespace std;
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int x = 5, y = 10;
float a = 5.5, b = 10.5;
double p = 5.55, q = 10.55;
cout << "Addition of " << x << " and " << y << " is " << add(x, y) << endl;
cout << "Addition of " << a << " and " << b << " is " << add(a, b) << endl;
cout << "Addition of " << p << " and " << q << " is " << add(p, q) << endl;
return 0;
}
Q20: Check input character is alphabet, digit or special character. (65-90 ,97-122,
(a-z, A-Z)48-57(0-9).
Ans:- #include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
cout << ch << " is an alphabet." << endl;
} else if (ch >= '0' && ch <= '9') {
cout << ch << " is a digit." << endl;
} else {
cout << ch << " is a special character." << endl; }
return 0; }
Q34: WAP to find the sum of series. 1-2+3-4………..n. Take input n from the user
Ans:- #include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter the value of n: ";
cin >> n;