You are on page 1of 7

Object Oriented

Programming
Assignment-1

Abubakar Mahboob
Program#1:
Write a program that calculates the sum of three integer values, the sum of three float values as well
as the sum of two integer and one float value with the help of 3 functions with the same name
calculate. take all the from user.

Solution:
#include <iostream>

using namespace std;

int sum (int,int,int);

float sum (float, float , float);

float sum (int, int , float);

int main(){

int num1, num2, x;

float num3, num4, y;

cout<<"Enter three integer numbers: ";

cin>>num1>>num2>>x;

cout<<"Result: "<<sum(num1, num2, x)<< endl;

cout<<"Enter three float numbers: ";

cin>>num3>>num4>> y;

cout<<"Result: " <<sum(num3, num4 , y)<< endl;

cout<<"Enter two int and one float number: ";

cin>>x>>y;

cout<<"Result: " <<sum(num1,x,y)<< endl;

return 0;
}

int sum(int a, int b , int c){

return a+b+c;

float sum(float a, float b , float c){

return a+b+c;

float sum(int a, int b , float c){

return a+b+c;

Output:

Enter three integer numbers: 2


2
2
Result: 6
Enter three float numbers: 1.2
1.2
1.2
Result: 1.6
Enter two int and one float number: 2
2
1.2
Result: 5.2
Screenshot :
Program#2:
Calculate the area of a circle and a square. to do so, create a shape class and derive two
classes circle and square from it. create a function having the same name calculateArea() in
both the derived class. you have to use operator in main function for calling the function.

Solution:
#include<iostream>

using namespace std;

class Shape

public: double a;

void get_data () {

cin>>a;

virtual void display_area () = 0;

};

class Circle:public Shape {

public:

void display_area () {

cout<<"Area of Circle "<<3.14 *a*a<<endl;

};

class Square:public Shape{

public:

void display_area (){

cout<<"Area of Square "<<a*a<<endl;


}

};

int main(){

Circle t;

Shape *st = &t;

cout<<"Enter the radius of Circcle: ";

st->get_data();

st->display_area();

Square r;

Shape *sr = &r;

cout<<"Enter the number: ";

sr->get_data();

sr->display_area();

return 0;

Output:
Enter the radius of Circcle: 2
Area of Circle : 12.56
Enter the number: 2
Area of Square: 4
Screenshot :

You might also like