Complex: o o o o
Complex: o o o o
• Question: Write a Complex class to represent complex numbers. The class should have:
o Private attributes for the real and imaginary parts.
o Constructors for initializing these values.
o Member functions to add, subtract, and multiply two complex numbers.
o Overloaded << operator for outputting a complex number in the form "a + bi".
• Objective: This question reinforces encapsulation, constructors, and operator overloading.
• Question: Create a base class Shape with a pure virtual function area(). Then, implement derived classes
Circle, Rectangle, and Triangle, each overriding the area() function to return the area of the
specific shape.
o Add appropriate constructors and functions to set dimensions.
o Write a main function to dynamically create an array of pointers to Shape objects and calculate their areas.
• Objective: This introduces polymorphism and abstract classes.
• Question: Create a Polynomial class that represents a polynomial equation. The class should contain a
Term nested class with private attributes for the coefficient and exponent.
o Implement a function to add terms to the polynomial.
o Implement an evaluate function that takes a value of x and returns the evaluated result of the polynomial.
• Objective: Demonstrates class composition and handling mathematical expressions.
• Question: Create a Division class with a function to divide two numbers and throw an exception if the
denominator is zero.
o Catch the exception in main() and display an appropriate message.
• Objective: Introduces exception handling in mathematical contexts.
Page 1 of 9
7. Template Class for Mathematical Structures
• Question: Implement a template class Pair that can store any pair of mathematical objects (like two
integers or two matrices).
o Include a function to find the sum of the two elements if they are of numeric types.
• Objective: Introduces students to templates and generic programming.
• Question: Write a class Statistics that provides overloaded functions to calculate the mean, median,
and standard deviation for:
o A vector of integers.
o A vector of doubles.
o An array of integers (using pointers).
• Objective: Teaches function overloading, handling multiple data types, and mathematical functions in a
class.
• Question: Create a class Student with private attributes name, rollNumber, and grade.
o Implement public getter and setter functions for each attribute.
o In the main function, demonstrate creating a Student object, setting values, and printing them.
• Objective: Helps students understand encapsulation and the role of getters and setters.
Page 2 of 9
== Solutions ==
cpp
Copy code
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
cpp
Copy code
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
class Shape {
public:
virtual double area() const = 0; // Pure virtual function
Page 3 of 9
virtual ~Shape() = default;
};
public:
Circle(double r) : radius(r) {}
double area() const override {
return M_PI * radius * radius;
}
};
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() const override {
return length * width;
}
};
public:
Triangle(double b, double h) : base(b), height(h) {}
double area() const override {
return 0.5 * base * height;
}
};
int main() {
vector<Shape*> shapes = {new Circle(5), new Rectangle(4, 6), new
Triangle(3, 4)};
for (Shape* shape : shapes) {
cout << "Area: " << shape->area() << endl;
delete shape;
}
return 0;
}
cpp
Page 4 of 9
Copy code
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
private:
int rows, cols;
vector<vector<int>> mat;
public:
Matrix(int r, int c) : rows(r), cols(c), mat(r, vector<int>(c, 0))
{}
cpp
Copy code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
class Polynomial {
private:
class Term {
public:
double coefficient;
int exponent;
Term(double c, int e) : coefficient(c), exponent(e) {}
};
Page 5 of 9
vector<Term> terms;
public:
void addTerm(double coef, int exp) {
terms.push_back(Term(coef, exp));
}
cpp
Copy code
#include <iostream>
using namespace std;
class Vector {
private:
double x, y, z;
static int count;
public:
Vector(double x = 0, double y = 0, double z = 0) : x(x), y(y),
z(z) {
++count;
}
int Vector::count = 0;
cpp
Copy code
#include <iostream>
#include <stdexcept>
using namespace std;
Page 6 of 9
class Division {
public:
double divide(double numerator, double denominator) {
if (denominator == 0) throw runtime_error("Division by zero");
return numerator / denominator;
}
};
int main() {
Division div;
try {
cout << div.divide(10, 0) << endl;
} catch (const exception& e) {
cout << e.what() << endl;
}
return 0;
}
cpp
Copy code
#include <iostream>
using namespace std;
public:
Pair(T f, T s) : first(f), second(s) {}
T sum() const {
return first + second;
}
};
cpp
Copy code
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
Page 7 of 9
class Statistics {
public:
double mean(const vector<int>& data) { /* ... */ }
double mean(const vector<double>& data) { /* ... */ }
double mean(const int* data, int size) { /* ... */ }
// Implement median and standard deviation similarly
};
cpp
Copy code
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
double balance;
public:
BankAccount(int acc, double bal = 0) : accountNumber(acc),
balance(bal) {}
cpp
Copy code
#include <iostream>
using namespace std;
class Student {
private:
Page 8 of 9
string name;
int rollNumber;
char grade;
public:
void setName(const string& n) { name = n; }
void setRollNumber(int r) { rollNumber = r; }
void setGrade(char g) { grade = g; }
int main() {
Student s;
s.setName("John");
s.setRollNumber(1);
s.setGrade('A');
cout << "Name: " << s.getName() << ", Roll: " << s.getRollNumber()
<< ", Grade: " << s.getGrade() << endl;
return 0;
}
These implementations cover each question and fulfill the objective of introducing basic to intermediate
C++ OOP concepts.
Page 9 of 9