Labs CS201: Lab # 12 Problem Statement
Labs CS201: Lab # 12 Problem Statement
Lab # 12
Problem Statement:
You need to write a program in which the following concepts must be implemented.
Write a template function name “Add” which accepts two arguments of the same type int, float, double from the
user then add those arguments, return their sum and display them on the screen.
Write a class named as firstClass, also write another class named as secondClass which should be nested inside
the firstClass. Define a function named displayMessage inside the secondClass, this method should print the
message “Inside the second class” on the screen. In the main function create the object of the secondClass and
invoke the displayMessage method using the secondClass object.
Solution:
#include <iostream>
class firstClass {
public:
class secondClass {
public:
void displayMessage(){
};
};
template<class T>
T Add (T x, T y) {
return x + y;
int main() {
object.displayMessage();
cout<< "Addition of Two floating point Number is = " << Add(floatOne,floatTwo) <<endl;
cout<< "Addition of Two double Number is = " << Add(doubleOne , doubleTwo) <<endl;
return 0;
}
Lab # 11
Problem Statement:
Write a program in C++ that add two class objects by operator overloading “plus (+)” operator. You are
required to create a class named “MathClass“ and declare the class member and member functions. Also declare
a class data member named as “number” and a parameterized constructor which take one argument and
initializes the number. Define the ‘+’ operator overloaded function to add two object’s numbers. Also define
another member function named as “Display ()” that shows the calculation result. Create three class objects for
example: obj1, obj2 and result. Values are passed by calling the parameterized constructor in main () function.
Add two object values by calling the ‘+’ operator overloaded function and then call display () function using
obj1, obj2 and result.
Solution:
#include <iostream>
class MathClass
private:
int number;
public:
MathClass()
number = 0 ;
MathClass(int x)
number = x ;
MathClass temp;
temp.number= number+m.number;
return temp;
};
int main()
result.Display();
system("pause");
}
Lab # 10
Problem Statement:
Write a C++ program in which you have to implement a class “Rectangle”. The class will have the following
data members:
Double length
Double breadth
Double area
You have to implement a parameterized constructor of this class which takes the two parameters “length” and
“breadth” and then calculate the area of the rectangle. Write getter and setter methods or member functions of
the class. You have to write a printArea() function which will display the area of the rectangle. Also, you have
to write a friend function of the class named as friendofRect().
In main () function you have to display the area by the following ways:
1. First create object with parameterized constructor and then print the Area of the rectangle
2. Secondly create pointer to object with new keyword and parameterized constructor and then call print
the Area of the rectangle.
3. Update the values of length and breadth of first object using friend function and then print the Area of
the rectangle.
Solution:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Rectangle{
private:
double area;
public:
double length;
double breadth;
//Parameterized Constructor of the Class
Rectangle(double,double);
double Rectangle::getLength(){
return length;
}
double Rectangle::getBreadth(){
return breadth;
}
int main(){
Rectangle r1(7,3);
r1.printArea();
friendOfRect(r1);
r1.printArea();
system("pause");
}
Lab # 9
Problem Statement:
Write a C++ program in which implement a class named “Employee”. This class has the following data
members:
Solution:
#include <iostream>
//#include <string.h>
#include <cstring>
class Employee {
private:
char name[30];
char gender[10];
int age;
double id;
public:
Employee();
void setName(char[]);
void setGender(char[]);
void setAge(int);
void setId(double);
char* getName();
char* getGender();
int getAge();
double getId();
void display();
};
Employee::Employee()
strcpy(name, "Empty");
strcpy(gender, "Empty");
age = 0;
id = 0.0;
strcpy(name, Name);
strcpy(gender, Gender);
age = Age;
id = Id;
age = Age;
id = Id;
}
char* Employee::getName()
return name;
char* Employee::getGender()
return gender;
int Employee::getAge()
return age;
double Employee::getId()
return id;
void Employee::display()
{
int main () {
Employee e1;
e1.display();
e2.display();
e1.setName("Asif");
e1.setGender("Male");
e1.setAge(30);
e1.setId(162);
return 0;
}
Lab # 8
Problem Statement:
Write a program that overloads function named “myfunc” for integer, double and character data types. For
example, if an integer value is passed to this function, the message "using integer myfunc" should be printed
on screen, on passing a double type value, the message "using double myfunc" and on passing character value,
the message "using character myfunc" should get printed.
Solution:
#include <iostream>
main()
system("pause");
Int myfunc(int x)
return x;
Double myfunc(double y)
{
return y;
Char myfunc(char z)
return z;
}
Lab # 7
Problem Statement:
Write a program which declare two variables of integer type and take their values as input from user. Also,
perform the following operations on them and print their values.
Solution:
#include <iostream>
int main(){
cin>> var1;
cin>> var2;
}
Lab # 6
Problem Statement:
Solution:
#include <iostream>
struct MyStruct{
int i;
float f;
ms4 = {0,0.0};
MyStruct ms3;
return ms3;
}
main(){
cin>> ms1.i;
cin>> ms1.f;
cin>> ms2.i;
cin>> ms2.f;
system("pause");
}
Lab # 5
Problem Statement:
Write a program in which you need to declare an integer type matrix of size 4*4. In this program:
1. You should take input values from the users and store it in 4*4 matrix.
2. Display this matrix on the screen.
3. Also, Display the transpose of this matrix by converting rows into cols.
Solution:
#include <iostream>
main(){
int a[arraySize][arraySize];
readMatrix(a);
cout << "\n\n" << "The original matrix is: " << '\n';
displayMatrix(a);
transposeMatrix(a);
displayMatrix(a);
system("pause");
cout << "\n" << "Enter " << row << ", " << col << " element: ";
int temp;
a[row][col] = a[col][row];
}
Lab # 4
Problem Statement:
Note: For comparing both these arrays, the size should be same.
Solution:
#include <iostream>
#include <cstring>
// function definition
int check = 1;
int i = 0;
while(i<size){
if(arr1[i] != arr2[i]){
check = 0;
break;
i++;
}
if(check == 1)
else
int main() {
cin.getline(firstArray, sizeof(firstArray));
cin.getline(secondArray, sizeof(secondArray));
if(strlen(firstArray) == strlen(secondArray)){
else {
system("pause");
}
Lab # 3
Problem Statement:
Write a program in which you have to declare an integer array of size 10 and initializes it with numbers of your
choice. Find the maximum and minimum number from the array and output the numbers on the screen.
For finding the maximum and minimum numbers from the array you need to declare two functions findMax
and findMin which accept an array and size of array (an int variable) as arguments and find the max min
numbers, and return those values.
Solution:
#include <iostream>
using namespace std;
int main() {
int number[10] = {
21,25,89,83,67,81,52,100,147,10
};
return 0;
int min = 0;
min = array[0];
min = array[i];
}
return min;
int max = 0;
max = array[0];
max = array[i];
return max;
}
Lab #2
Problem Statement:
Write a program in which you have to define a function displayDiagnol which will have two integer arguments
named rows and cols. In the main function, take the values of rows and columns from the users. If the number
of rows is same as numbers of columns then call the displayDiagnol function else show a message on screen
that number of rows and columns is not same.
The function will take the value of rows and cols which are passed as argument and print the output in matrix
form. To print the values in the matrix form, nested loops should be used. For each loop, you have to use a
counter variable as counter. When the value of counters for each loop equals, then it prints the value of row at
that location and prints hard coded zero at any other location.
Example if the user enters rows and cols as 3, then the output should be like this
100
020
003
Solution:
#include <iostream>
using namespace std;
rows = 0;
columns = 0;
cin>> rows;
cin>> columns;
if(rows == columns)
else
return 0;
// function definition
if(i==j)
else
cout<< 0 << " ";
cout<< "\n";
}
Lab # 1
Problem Statement:
“Calculate the average age of a class of ten students. Prompt the user to enter the age of each student.”
“Prompt the user to enter the age of each student” this requires cin>> statement.
For example:
cin>> age1;
Average can be calculated by doing addition of 10 variables and dividing sum with 10.
TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 +age9 + age10 ;
Solution:
#include<iostream>
main() {
int age1 = 0, age2 = 0, age3 = 0, age4 = 0, age5 = 0, age6 = 0, age7 = 0, age8 = 0, age9 = 0, age10 = 0;
cin>>age1;
cin>>age2;
cout<<"please enter the age of student 3: ";
cin>>age3;
cin>>age4;
cin>>age5;
cin>>age6;
cin>>age7;
cin>>age8;
cin>>age9;
cin>>age10;
TotalAge = age1 + age2 + age3 + age4 + age5 + age6 + age7 + age8 + age9 + age10;
AverageAge = TotalAge/10;