Level 1

1.

Create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.

Answer

```
#include <iostream>
using namespace std;
#include <string>
class Student
{
public:
string name;
int roll_no;
};
int main()
{
Student s;
s.name = "Jhon";
s.roll_no = 2;
cout << s.name << " " << s.roll_no << endl;
return 0;
}
```

2.

Assign and print the roll number, phone number and address of two students having names "Sam" and "John" respectively by creating two objects of the class 'Student'.

3.

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' with a function to print the area and perimeter.

Answer

```
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Triangle
{
public:
void print_area(int s1, int s2, int s3)
{
double s = (s1+s2+s3)/2.0;
cout << s << endl;
cout << "Perimeter is " << (s1+s2+s3) << endl;
}
};
int main()
{
Triangle t;
t.print_area(3,4,5);
return 0;
}
```

4.

Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' with the constructor having the three sides as its parameters.

Answer

```
#include <iostream>
#include <string>
#include <cmath>
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.0;
cout << s << endl;
cout << "Perimeter is " << (s1+s2+s3) << endl;
}
};
int main()
{
Triangle t(3,4,5);
t.print_area();
return 0;
}
```

5.

Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by creating a class named 'Rectangle' with a function named 'Area' which returns the area. Length and breadth are passed as parameters to its constructor.

6.

Write a program to print the area of a rectangle by creating a class named 'Area' having two functions. First function named as 'setDim' takes the length and breadth of the rectangle as parameters and the second function named as 'getArea' returns the area of the rectangle. Length and breadth of the rectangle are entered through keyboard.

Answer

```
#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;
}
```

7.

Write a program to print the area of a rectangle by creating a class named 'Area' taking the values of its length and breadth as parameters of its constructor and having a function named 'returnArea' which returns the area of the rectangle. Length and breadth of the rectangle are entered through keyboard.

8.

Print the average of three numbers entered by the user by creating a class named 'Average' having a function to calculate and print the average without creating any object of the Average class.

9.

Print the sum, difference and product of two complex numbers by creating a class named 'Complex' with separate functions for each operation whose real and imaginary parts are entered by the user.

Answer

```
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(int r, int i)
{
real = r;
imag = i;
}
int get_real()
{
return real;
}
int get_imag()
{
return imag;
}
void add(Complex c1)
{
cout << c1.get_real()+real << "+i" << c1.get_imag()+imag << endl;
}
void difference(Complex c1)
{
cout << real-c1.get_real() << "+i" << imag-c1.get_imag() << endl;
}
void multiply(Complex c1)
{
cout << ((real*c1.get_real())-(imag*c1.get_imag())) << "+i" << ((real*c1.get_imag())+(imag*c1.get_real())) << endl;
}
};
int main()
{
Complex c1(4,5);
Complex c2(2,3);
c1.add(c2);
c1.difference(c2);
c1.multiply(c2);
return 0;
}
```

10.

Write a program to print the volume of a box by creating a class named 'Volume' with an initialization list to initialize its length, breadth and height. (just to make you familiar with initialization lists)

11.

Write a program that would print the information (name, year of joining, salary, address) of three employees by creating a class named 'Employee'. The output should be as follows:

Name Year of joining Address

Robert 1994 64C- WallsStreat

Sam 2000 68D- WallsStreat

John 1999 26B- WallsStreat

Name Year of joining Address

Robert 1994 64C- WallsStreat

Sam 2000 68D- WallsStreat

John 1999 26B- WallsStreat

12.

Add two distances in inch-feet by creating a class named 'AddDistance'.

Level 2

1.

Write a program by creating an 'Employee' class having the following functions and print the final salary.

1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as parameters

2 - 'AddSal()' which adds $10 to the salary of the employee if it is less than $500.

3 - 'AddWork()' which adds $5 to the salary of the employee if the number of hours of work per day is more than 6 hours.

1 - 'getInfo()' which takes the salary, number of hours of work per day of employee as parameters

2 - 'AddSal()' which adds $10 to the salary of the employee if it is less than $500.

3 - 'AddWork()' which adds $5 to the salary of the employee if the number of hours of work per day is more than 6 hours.

2.

Create a class called 'Matrix' containing constructor that initializes the number of rows and the number of columns of a new Matrix object. The Matrix class has the following information:

1 - number of rows of matrix

2 - number of columns of matrix

3 - elements of matrix (You can use 2D vector)

The Matrix class has functions for each of the following:

1 - get the number of rows

2 - get the number of columns

3 - set the elements of the matrix at a given position (i,j)

4 - adding two matrices.

5 - multiplying the two matrices

You can assume that the dimensions are correct for the multiplication and addition.

1 - number of rows of matrix

2 - number of columns of matrix

3 - elements of matrix (You can use 2D vector)

The Matrix class has functions for each of the following:

1 - get the number of rows

2 - get the number of columns

3 - set the elements of the matrix at a given position (i,j)

4 - adding two matrices.

5 - multiplying the two matrices

You can assume that the dimensions are correct for the multiplication and addition.

Answer

```
#include <iostream>
#include <vector>
using namespace std;
class Matrix
{
private:
int row,col;
vector<vector<int>> matrix;
public:
Matrix(int r, int c, vector<vector<int>> &m)
{
row = r;
col = c;
matrix = m;
}
int get_row_number()
{
return row;
}
int get_col_number()
{
return col;
}
vector<vector<int>> get_vector()
{
return matrix;
}
void set_element(int i, int j, int e)
{
matrix[i][j] = e;
}
void display()
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
Matrix add(Matrix m)
{
//assuming matrices can be added
vector<vector<int>> v;
v.resize(row,vector<int>(col,0));
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
v[i][j] = matrix[i][j]+m.get_vector()[i][j];
}
}
Matrix n(row,col,v);
return n;
}
Matrix multiply(Matrix m)
{
//assuming dimension is correct for multiplication
vector<vector<int>> v;
v.resize(row,vector<int>(m.get_col_number(),0));
for(int i=0; i<row; i++)
{
for(int j=0; j<m.get_col_number(); j++)
{
for(int k=0; k<col; k++)
{
v[i][j] = v[i][j]+(matrix[i][k]*m.get_vector()[k][j]);
}
}
}
Matrix n(row,m.get_col_number(),v);
return n;
}
};
int main()
{
vector<vector<int>> m{{1,2,3},{4,5,6},{7,8,9}};
vector<vector<int>> n{{10,11,12},{13,14,15},{16,17,18}};
Matrix m1(3,3,m);
Matrix m2(3,3,n);
m1.display();
m2.display();
Matrix a = m1.add(m2);
a.display();
Matrix b = m1.multiply(m2);
b.display();
return 0;
}
```

Recent Posts

Beginning with ML 2.0: Multivariate Linear Regression

Beginning with Machine Learning: Linear Regression

Scrolling Progress Indicator

Getting Notebook Paper Effect with CSS