Question

c++

Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { publPart 2 Implement constructors for each of the three derived classes above. Each constructor needs to call the base class cons

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

//Source code

#include<iostream>
using namespace std;
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() true
{
   return false;
}
class IDcard :public Card
{
   public:
       IDcard();
       IDcard(string n,string id);
       virtual void print() const;
   private:
       string IDnumber;
};
IDcard::IDcard():Card()
{
   IDnumber = "";
}
IDcard::IDcard(string n,string id):Card(n)
{
   IDnumber = id;
}
void Card::print() const
{
   cout<<"Name: "<< name<<endl;
}
void IDcard::print() const
{
   Card::print();
   cout<<"ID number: "<<IDnumber<<endl;
}
int main()
{
   Card c("John"); // Creating object 'c' for Card class
   IDcard d("Ana","12345"); // Creating object 'd' for IDCard class
   c.print(); // calling print function for Card class object 'c'
   d.print(); // calling print function for IDCard class object 'd'
   return 0;
}

Screen shot of the program

1 2 6 7 inheritence.cpp #include<iostreant using namespace std; 3 class Card 49 { 5 public: Card(); Card(string n); 8 virtual

| } } inheritence.cpp 23 return false; 24 25 class IDcard : public Card 26 { 27 public: 28 IDcard(); 29 IDcard(string n, stri

Screen shot of the output

O X D:\visaual C+ + \inheritence.exe Name : John Name : Ana ID number: 12345 Process exited after 9.006497 seconds with retur

Please comment if any query

Add a comment
Know the answer?
Add Answer to:
c++ Part 1 Consider using the following Card class as a base class to implement a...
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
  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • 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...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • 1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7...

    1 #include <string> 2 #include <iostream> 3 using std::string; 4 using std::cout; 5 using std::endl; 7 class Pet 8 { 9   public: 10       string name; 11       virtual void print( ) const; 12}; 13 14 class Dog:public Pet 15 { 16   public: 17       string breed; 18       virtual void print( ) const; 19}; 20 21 int main( ) 22 { 23   Dog vdog; 24   Pet vpet; 25   vdog.name = "Tiny"; 26   vdog.breed = "Great Dane"; 27   vpet =...

  • #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const;...

    #code for creature.cpp #include <iostream> using namespace std; class Creature { public: Creature(); void run() const; protected: int distance; }; Creature::Creature(): distance(10) {} void Creature::run() const { cout << "running " << distance << " meters!\n"; } class Wizard : public Creature { public: Wizard(); void hover() const; private: int distFactor; }; Wizard::Wizard() : distFactor(3) {} void Wizard::hover() const { cout << "hovering " << (distFactor * distance) << " meters!\n"; } //Created new derived class from Creature class Widget...

  • C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement...

    C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...

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