Question

Need to use C++ classes and each class will have 2 data members. We will provide...

Need to use C++ classes and each class will have 2 data members. We will provide the code in main(), as well as a menu-driven "driver" section of code that will allow testing portions of your code separately, to give the option for partial credit.  

Sample application areas are:

  1. class Movie: int year, char genre ('F' for fiction, 'N' for non-fiction)
  2. class Product: int price, char name ('I' for imported, 'L' for local)
  3. class Shape: int points, char name ('S' for Square, 'C' for Circle)
  4. class Apartment: int aptNumber, char aptType ('S' for studio , 'B' for 1 bedroom)

Each class will need to have the following:

  1. get() and set() methods for the data members
  2. Default and Fully-qualified constructors
  3. Copy constructor
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given that menu driven main program will be given. So i have written the code for classes and their invocation in main().

C++ Program:

#include <iostream>
using namespace std;

class Movie
{
private:
int year;
char genre;
public:
//Default Constructor
Movie(){
year = 2000;
genre = 'F';
}
  
//Fully initialized parameter Constructor
Movie(int y,char g){
year = y;
genre = g;
}
  
//Copy Conctructor
Movie(const Movie &m){
year = m.year;
genre = m.genre;
}
  
//Setter and getter methods
  
int getYear(){
return year;
}
  
void setYear(int y){
year = y;
}
  
char getGenre(){
return genre;
}
  
void setGenre(char g){
genre = g;
}
  
void display(){
cout<<"Movie Object: Year:"<<year<<" Genre: ";
if(genre=='F')
cout<<"Fiction";
else
cout<<"Non-Fiction";
cout<<endl;
}
};

class Product
{
private:
int price;
char name;
public:
//Default Constructor
Product(){
price = 20000;
name = 'I';
}
  
//Fully initialized parameter Constructor
Product(int p,char n){
price = p;
name = n;
}
  
//Copy Conctructor
Product(const Product &p){
price = p.price;
name = p.name;
}
  
//Setter and getter methods
  
int getPrice(){
return price;
}
  
void setPrice(int p){
price = p;
}
  
char getName(){
return name;
}
  
void setName(char n){
name = n;
}
  
void display(){
cout<<"Product Object: Price:"<<price<<" Name: ";
if(name=='I')
cout<<"Imported";
else
cout<<"Local";
cout<<endl;
}
};

class Shape
{
private:
int points;
char name;
public:
//Default Constructor
Shape(){
points = 20;
name = 'C';
}
  
//Fully initialized parameter Constructor
Shape(int p,char n){
points = p;
name = n;
}
  
//Copy Conctructor
Shape(const Shape &s){
points = s.points;
name = s.name;
}
  
//Setter and getter methods
  
int getPoints(){
return points;
}
  
void setPoints(int p){
points = p;
}
  
char getName(){
return name;
}
  
void setName(char n){
name = n;
}
  
void display(){
cout<<"Shape Object: Points:"<<points<<" Name: ";
if(name=='S')
cout<<"Square";
else
cout<<"Circle";
cout<<endl;
}
};

class Apartment
{
private:
int aptNumber;
char aptType;
public:
//Default Constructor
Apartment(){
aptNumber = 20;
aptType = 'B';
}
  
//Fully initialized parameter Constructor
Apartment(int a,char type){
aptNumber = a;
aptType = type;
}
  
//Copy Conctructor
Apartment(const Apartment &a){
aptNumber = a.aptNumber;
aptType = a.aptType;
}
  
//Setter and getter methods
  
int getAptNumber(){
return aptNumber;
}
  
void setAptNumber(int a){
aptNumber = a;
}
  
char getAptType(){
return aptType;
}
  
void setAptType(char type){
aptType = type;
}
  
void display(){
cout<<"Apartment Object: aptNumber:"<<aptNumber<<" Apt Type: ";
if(aptType=='S')
cout<<"Studio";
else
cout<<"1 Bedroom";
cout<<endl;
}
};


int main() {
   Movie m(2004,'F');
   m.display();
   Product p(2000,'L');
   p.display();
   Shape s(10,'S');
   s.display();
   Apartment a(443,'S');
   a.display();
   //Copy Constructor invocation
   Shape s2 = s;
   s2.display();
   return 0;
}

Output:

Output: Copy Movie Object: Year:2004 Genre: Fiction Product Object: Price: 2000 Name: Local Shape Object: Points:10 Name: Squ

Add a comment
Know the answer?
Add Answer to:
Need to use C++ classes and each class will have 2 data members. We will provide...
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
  • Write a class called Taxicab that has three int fields (data members) to store its current...

    Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • Question 16 You must put your data in classes if you use C++. True False 2...

    Question 16 You must put your data in classes if you use C++. True False 2 points Question 17 Constructors are automatically invoked during class instantiation. True False 2 points Question 18 To make your data accessible to entities outside of your class, declare the members of that class as private. True False 2 points Question 19 You can make arrays of built-in types like int and double, but not of user-defined types. True False 2 points Question 20 One...

  • Please use C++ Suppose we have the following book base class: class book { public: book(char*,...

    Please use C++ Suppose we have the following book base class: class book { public: book(char*, char *, int); void show_book(); private: char title[64]; char author[64]; int pages; }; Suppose that we need to derive a new class, called libraryCard class, which adds the following members to the book class: char catalog[64]; int checked_out; // 1 if checked out, otherwise 0 if available libraryCard(char*, char *, int, char *, int); void show_card(); 1. Derive the new class from the base...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • C++ program Suppose we have the following book base class: class book { public: book(char *,...

    C++ program Suppose we have the following book base class: class book { public: book(char *, char *, int); void show_book(); private: char title[64]; char author[64]; int pages; }; Suppose that we need to derive a new class, called libraryCard class, which adds the following members to the book class: char catalog[64]; int checked out; // 1 if checked out, otherwise 0 if available libraryCard(char *, char *, int, char *, int); void show_card(); 1. Derive the new class from...

  • c++ please need help with this question Consider a class Employee with data members: age(an integer),...

    c++ please need help with this question Consider a class Employee with data members: age(an integer), id (an integer) and salary (a float), and their corresponding member functions as follows: class Employee {        private:             int age;             int id;             float salary;        public:             Employee( ); // default constructor: age=0, id=0, and salary=0             Employee(Employee &x);   // copy constructor           Employee& operator = (Employee &x); // equal sign operator             void setAge(int x);    // let age = x...

  • Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will...

    Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will be called TimeOff. This class makes use of another class called NumDays. Then you will create a driver (main) that will handle the personnel records for a company and generate a report. TimeOff class will keep track of an employee’s sick leave, vacation, and unpaid time off. It will have appropriate constructors and member functions for storing and retrieving data in any of the...

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