OOP Quiz Solution - Write a program that has an abstract base class named Quad., Quizzes of Object Oriented Programming

Write a program that has an abstract base class named Quad. This class should have for member data variables (floats) representing side length and a pure virtual function Area. It should also have a method for setting the data variables. Derive a class Rectangle from Quad and override the Area method so that it returns the area of the Rectangle. Write a main function that creates a Rectangle and sets the side lengths. Also, write a top­level function that will take a parameter of type Quad and r

Typology: Quizzes

2022/2023

Available from 12/09/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q.1 (Marks 5)
Write a program that has an abstract base class named Quad. This class should have
four member data variables (floats) representing side length and a pure virtual function Area. It should
also have a method for setting the data variables. Derive a class Rectangle from Quad and override the Area method
so that it returns the area of the Rectangle. Write a main function that creates a Rectangle and sets the side lengths.
Also write a toplevel function that will take a parameter of type Quad and return the value of the appropriate
Area function.
#include<iostream>
using namespace std;
class quad
{
public:
float area;
float length;
float width;
virtual float get_area()=0;
};
class Rectangle: public quad{
public:
float set_length(float l){
length=l;}
float get_length(){
return length;}
float set_width(float w){
width=w;}
float get_width()
{return width;}
float get_area(){
return length*width; }
};
int main()
{
Rectangle r1;
r1.set_length(3.5);
r1.set_width(4.3);
cout<<r1.get_area();
return 0;
}
pf2

Partial preview of the text

Download OOP Quiz Solution - Write a program that has an abstract base class named Quad. and more Quizzes Object Oriented Programming in PDF only on Docsity!

Q.1 (Marks 5) Write a program that has an abstract base class named Quad. This class should have four member data variables (floats) representing side length and a pure virtual function Area. It should also have a method for setting the data variables. Derive a class Rectangle from Quad and override the Area method so that it returns the area of the Rectangle. Write a main function that creates a Rectangle and sets the side lengths. Also write a toplevel function that will take a parameter of type Quad and return the value of the appropriate Area function.

#include

using namespace std;

class quad

public:

float area;

float length;

float width;

virtual float get_area()=0;

class Rectangle: public quad{

public:

float set_length(float l){

length=l;}

float get_length(){

return length;}

float set_width(float w){

width=w;}

float get_width()

{return width;}

float get_area(){

return length*width; }

int main()

Rectangle r1;

r1.set_length(3.5);

r1.set_width(4.3);

cout<<r1.get_area();

return 0;

  • Object Oriented Programming Page 1 of