Question

For C++

2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by i

This is the information about the meal class

Question 1 - Abstract Class 1-1. (24 marks) Define and implement a class named Meal that has the following constructor and at

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

Sreenshot

Mains.cpp Mains.h Dessert.cpp Dessert.h Meal.cpp Meal.h MealDesertMain.cpp + X + MealDesert Main (Global Scope) maino 5 #incl

Program

Meal.h

#include<iostream>
#include<string>
using namespace std;
//Create a class Meal
class Meal {
//Member variables
protected:
   string name;
   string type;
   int cost;
   string mealID;
   static int mealCounter;
//Member functions
public:
   //Default constructor
   Meal();
   //Parameterized constructor
   Meal(string n, string t, int c);
   //Setters
   void set_name(string n);
   void set_type(string t);
   void set_cost(int c);
   //Getters
   string get_name();
   string get_type();
   string get_mealID();
   virtual int get_cost()=0;
};

Meal.cpp

//Implementation of meal class
#include "Meal.h"
//Default constructor
Meal::Meal() {
   name = type = mealID = "";
   cost = 0;
   mealCounter++;
}
//Parameterized constructor
Meal::Meal(string n, string t, int c) {
   name = n;
   type = t;
   cost = c;
   mealID = type + to_string(mealCounter);
   mealCounter++;
}
//Setters
void Meal::set_name(string n) {
   name = n;
}
void Meal::set_type(string t) {
   type = t;
}
void Meal::set_cost(int c) {
   cost = c;
}
//Getters
string Meal::get_name() {
   return name;
}
string Meal::get_type() {
   return type;
}
string Meal::get_mealID() {
   return mealID;
}

Dessert.h

#include "Meal.h"
//Create a class Dessert
class Dessert:public Meal {
private:
   static int nextID;
public:
   //Constructor
   Dessert(string n, int c);
   int get_cost();
};


Dessert.cpp

//Implementation of dessert
#include "Dessert.h"
int Dessert::nextID = 0;
Dessert::Dessert(string n, int c):Meal(n,"dessert",c) {
   nextID++;
}
int Dessert::get_cost() {
   if (mealCounter < 1) {
       return cost;
   }
   else {
       return mealCounter * cost;
   }
}

Mains.h

#include "Meal.h"
class Mains :public Meal {
private:
   static int nextID;
   int m;
   int list[];
   void sort();
public:
   //Constructor
   Mains(string n, int co, int *list, int m);
   //search an item
   int search(int x);
   int get_cost();
};

Mains.cpp

#include "Mains.h"
int Mains::nextID = 500;
//Constructor
Mains::Mains(string n, int co, int *list, int m):Meal(n,"mains",co) {
   nextID++;
   this->m = m;
   list = new int[m];
   for (int i = 0; i < m; i++) {
       this->list[i] = list[i];
   }
}
void Mains::sort() {
   for (int i = 0; i < m; i++) {
       for (int j = i + 1; j < m; j++) {
           if (list[i] > list[j]){
               int temp = list[i];
               list[i]=list[j];
               list[j]=temp;
           }
       }
   }
}
//search an item
int Mains::search(int x) {
   sort();
   for (int i = 0; i < m; i++) {
       if (list[i] == x) {
           return i;
       }
   }
   return -1;
}
int Mains::get_cost() {
   if (m >= 0 && m <= 2) {
       return cost;
   }
   else {
       int t = 0;
       for (int i = 0; i < m; i++) {
           t += list[i] * cost;
       }
       return t;
   }
}

Main.cpp

#include "Meal.h"
#include "Mains.h"
#include "Dessert.h"
int Meal::mealCounter = 0;
int main()
{
    //Create a dessert
   Dessert d1("Ice cream", 10);
   cout << d1.get_cost() << endl;
   Dessert d2("Choco Chip Delight", 10);
   cout << d2.get_cost() << endl;
   int list[] = { 1,2 };
   Mains main("Two dessert", 10, list, 2);
   cout << main.get_mealID() << ", " << main.get_cost() << endl;
   return 0;
}

Output

10
20
mains2, 10

Note

Please add your test information and what output are you expecting. It is difficult for me to figure out what you mean by this 3 classes. I hope this is what you expect.

Add a comment
Know the answer?
Add Answer to:
For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...
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
  • c++ please    Define the class bankAccount to implement the basic properties of a bank account....

    c++ please    Define the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder’s name (string), account number (int), account type (string, checking/saving), balance (double), and interest rate (double). (Store interest rate as a decimal number.) Add appropriate member func- tions to manipulate an object. Use a static member in the class to automatically assign account numbers. Also declare an array of 10 compo- nents of...

  • Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides...

    Question 1 (24 Marks] 1. (4 marks) Write an immutable Person class in Java which provides the following. • Two attributes: lastName and first Name of type String. • Appropriate initialization, accessors/mutator methods. 2. (6 marks) Write a Student class that is a subclass of Person class (in the previous question) which provides the following. • Two attributes: A unique student ID of type String and GPA of type float; the student ID is initialized when a Student object gets...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • In c++ please and a correct answer please because last time some guy gave me a...

    In c++ please and a correct answer please because last time some guy gave me a wrong code. Thanks Part 1 - Simple Classes and Class Variables 1-1 Define and implement a class named animal that has the following public constructors and behaviours animal (string aSpecies); // animals are allocated a unique ID on creation, // the first animal has ID 1, the second animal is 2 and so on void set_name(string aName); // change the animal's name string get_speciesO;...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...

  • The class Engineer Test below is used to test two other class named Project & Engineer....

    The class Engineer Test below is used to test two other class named Project & Engineer. A project has three attributes: projNo (int), projName (string), revenue (double). Add a parameterized constructor, and setters & getters for all attributes. An engineer has four attributes: jobld (int), name (string), rate (double), and a list of projects. For each project, the engineer has a specific amount as profit (project revenue rate). Add a constructor that initializes the attributes: jobild, name and rate. Implement...

  • The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and...

    The FoodItem.java file defines a class called FoodItem that contains instance variables for name (String) and calories (int), along with get/set methods for both. Implement the methods in the Meal class found in Meal.java public class FoodItem{ private String name; private int calories;    public FoodItem(String iName, int iCals){ name = iName; calories = iCals; }    public void setName(String newName){ name = newName; }    public void setCalories(int newCals){ calories = newCals; }    public int getCalories(){ return calories;...

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
ADVERTISEMENT