Question

please help code in c++, assignment stats below dashed line ________________________________ In this lab we will...

please help code in c++, assignment stats below dashed line

________________________________

In this lab we will practice polymorphism using several different classes. Implement your classes in B.h and test them in main.cpp:

B.h

  • Define a class B1 with:
    • virtual function vf()
    • non-virtual function f()
    • pure virtual function pvf()
    • Define these functions within class B1
    • Implement each function to output its name (e.g., B1::vf()).
    • Make the functions public.
  • Derive a class D1 from B1
    • Define f()
    • override vf()
    • override pvf()
  • Derive a class D2 from D1
    • override pvf()
  • Define a class B2 with a pure virtual function pvf()
  • Derive a class D21 from B2 with a string data member and a member function that overrides pvf()
    • Initialize the string to "D21::pvf()"
    • D21::pvf() should output the value of the string and a newline
  • Define a class D22 that is just like D21 except that its data member is an int
    • Initialize the int to 22
    • Your output only needs to output the integer value and a newline
  • Define a global function f() that takes a B2& argument and calls pvf() for its argument.

main.cpp

  • Try to create a B1 object. What does the compiler tell you? Why?
  • Make a D1 object and call f(), vf(), and pvf() for it
  • Define a reference to B1 (a B1&) and initialize that to the D1 object you just defined.
  • Call f(), vf(), and pvf() for that reference.
  • Make an object of class D2 and call f(), vf(), and pvf() for it.
  • Make a D21 and a D22 object
  • Call f() with a D21 and a D22
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

#include<iostream>
using namespace std;
//Define a class B1 with:
//virtual function vf()
//non-virtual function f()
//pure virtual function pvf()
class B1{
public:
virtual void vf();
void f();
virtual void pvf()=0;
};
void B1::vf()
{
cout<<"This is B1's virtual function "<<endl;
}
void B1::f()
{
cout<<"This is B1's non-virtual function"<<endl;
}
//Derive a class D1 from B1
//Define f()
//override vf()
//override pvf()
class D1 : public B1
{
public :
void f()
{
cout<<"This is D1's non-virtual function"<<endl;
}
void vf()
{
cout<<"This is D1's Overridden virtual function"<<endl;
}
void pvf()
{
cout<<"This is D1's Overridden pure-virtual function"<<endl;
}

};
//Derive a class D2 from D1
//override pvf()
class D2 : public D1
{
public:
void pvf()
{
cout<<"This is D2's Overridden pure virtual function"<<endl;
}
};
//Define a class B2 with a pure virtual function pvf()
class B2
{
public :
virtual void pvf()=0;
};

//Derive a class D21 from B2 with a string data member and a member function that overrides pvf()
//Initialize the string to "D21::pvf()"
class D21 :public B2
{
private:
string name="D21::pvf()";
public:
void pvf();
};
void D21::pvf()
{
cout<<name<<endl;
}
//Define a class D22 that is just like D21 except that its data member is an int
//Initialize the int to 22
class D22:public B2
{
private :
int data=22;
public:
void pvf();

};
void D22::pvf()
{
cout<<data<<endl;
}
//Define a global function f() that takes a B2& argument and calls pvf() for its argument.
void f(B2& b2)
{
b2.pvf();
}
//*****************************************main.cpp *******************************************

#include <iostream>
#include "B.h"
using namespace std;
int main()
{
//Make a D1 object and call f(), vf(), and pvf() for it
D1 d1;
d1.f();
d1.vf();
d1.pvf();
//Define a reference to B1 (a B1&) and initialize that to the D1 object you just defined.
B1* b1;
b1=&d1;
//Call f(), vf(), and pvf() for that reference.
b1->f();
b1->vf();
b1->pvf();
//Make an object of class D2 and call f(), vf(), and pvf() for it.
D2 d2;
d2.f();
d2.vf();
d2.pvf();
//Make a D21 object
D21 d21;
//Call pvf() with a D21
d21.pvf();
//Make a D21 object
D22 d22;
//Call pvf() with a D21
d22.pvf();
return 0;
}
//Ouput

Q.Try to create a B1 object. What does the compiler tell you? Why?

Answer: We can not create B1 object ,Compiler will give your error because this is a abstract class due to pure virtual function .We can't create object of abstract class.

CAUsershpDocumentscodeBlockogramabc.exe This is D1's non-virtual function This is D1's Overridden virtual function This is D1's Overridden pure-virtual function This is B1's non-virtual function This is D1's Overridden virtual function This is D1's Overridden pure-virtual function This is D1's non-virtual function This is D1's Overridden virtual function This is D2's Overridden pure virtual function D21: :pvfO Process returned e (ex execution time e.362 s Press any key to continue

Add a comment
Know the answer?
Add Answer to:
please help code in c++, assignment stats below dashed line ________________________________ In this lab we will...
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
  • Please implement the following problem in basic C++ code and include detailed comments so that I...

    Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...

  • C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...

    C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to: Set counter to the integer value specified by the user. Initialize counter to 0. Return the value of counter with a function named getCounter. Increment and...

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

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

  • previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print...

    previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() {    //create Animal object    Animal animal("Dog","Mammal",4);    cout<<"Animal object details"<<endl;    cout<<"Name: "<<animal.getName()<<endl;    cout<<"Type: "<<animal.getType()<<endl;    cout<<"# of Legs: "<<animal.getLegs()<<endl;          cout<<endl<<endl;    //create Cat object    Cat cat("byssinian cat","carnivorous mammal",4,"short...

  • .Your solution must include header, implementation file, and test files .In C++ write a code to...

    .Your solution must include header, implementation file, and test files .In C++ write a code to Consider a graphics system that has classes for various figures rectangles, squares, triangles, circles, and so on. For example, a rectangle might have data members for Height, Width and center point, while a square and circle might have only a center point and an edge length or radius. In a well-designed system, these would be derived from a common class, Figure. You are to...

  • Second time posting please answer this question!!! Thankyou!! Please do it in visual studio C #...

    Second time posting please answer this question!!! Thankyou!! Please do it in visual studio C # and post copy of form.cs and designer.cs. Will give vgood review!!! In this module you learned about Classes and Multiforms. You will be completing one program for this module Write a class named Car that has the following member variables: year -    An int that holds the car’s model year. make - A string object that holds the make of the car. speed -...

  • C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names...

    C++ PLEASE Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient. The header file for this class is given below. #ifndef LETTER_H #define LETTER_H #include #include using namespace std; class Letter { public:    //constructor    Letter(string from = "John", string to = "Ana");    void add_line(string line);    string get_text() const; private:    string sender;    string recipient;    vector lines; }; #endif -Implement the constructor to initialize...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from lab 4.1, add the ability to read from a file. Modify the input function:       * Move the input function out of the Cargo class to just below the end of the Cargo class       * At the bottom of the input function, declare a Cargo object named         temp using the constructor that takes the six parameters.       * Use the Cargo output...

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