Question

Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code:

#include "stdafx.h"

#include <iostream>

using namespace std;

//base class Sale

class Sale

{

//variable and function declaration

public:

double price;

public:

//constructor

Sale(double p)

{

   price = p;

}

virtual void bill(){};

};

//Derived class

class MailorderSale : public Sale

{

private:

double shipping_charge;

public:

MailorderSale(double p, double sc) : Sale(p)

{

   shipping_charge = sc;

}

void bill()

{

   price = price + shipping_charge;

};

};

//Class for Discount

class DiscountSale : public Sale

{

private:

double discount;

public:

DiscountSale(double p, double d):Sale(p)

{

discount = d;

}

void bill()

{

price = price - discount;

};

};

//main

int main()

{

    DiscountSale discount(11,10);

   MailorderSale mail(8,2);

   if ( mail.price < discount.price)

   {

       cout << "\n Mailorder price less than Discount price" << endl;

   }

   else

   {

       cout << "\nMailorder price greater than or equal to Discount price" << endl;

   }

    system("pause");

    return 0;

}

Output:

Mailorder price less than Discount price Press any key to continue . -

Add a comment
Know the answer?
Add Answer to:
Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a base class and two derived classes. Also, create and call a member function named...

    Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • solve it in c++10 Create a composition between the classes Job and Salary. Class Salary a....

    solve it in c++10 Create a composition between the classes Job and Salary. Class Salary a. data member:    1. money b. constructors:    1. default constructor    2. user defined constructor with a parameter to set money c. standard accessor and mutator functions Class Job a. data members:    title (name of the job)    salary (object of type Salary) b. constructors:    1. default constructor    2. user defined constructor to set title and salary    3. implement constructor delegation c. standard accessor and mutator function...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember...

    C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT