question 1. #include <iostream> using namespace std; //Abstract class becuase it contain ppure virtual function class Vehicle{ public: virtual void drive() = 0; }; class Car: public Vehicle{ public: void drive(){ cout<<"Driving a car "<<endl; } }; class Motorcycle: public Vehicle{ public: void drive(){ cout<<"Driving a motorcycle"<<endl; } }; int main() { //Abstract class can not be instantiated. but you can write as pointer and Vehicle *obj; Car c; obj = &c; obj->drive(); Motorcycle m; obj = &m; obj->drive(); return 0; } //OUTPUT: Driving a car Driving a motorcycle ____________________________________________________________ Question 2: #include <iostream> using namespace std; class Shape{ public: double Area(){ return 0.0; } }; class Rectangle: public Shape{ double width; double length; public: Rectangle(double width, double length){ this->width = 10; this->length = 10; } double Area(){ //int area = width * length; return width * length; //cout<<"Area of Rectangle = "<<area<<endl; } }; class Triangle: public Shape{ double base; double heigth; public: Triangle(double b, double h):base(b),heigth(h){ } double Area(){ return 0.5 * base * heigth; } }; int main() { Shape *shape; Rectangle rect(10, 10); shape = ▭ shape->Area(); return 0; } ________________________________________________________________- Question 3. #include <iostream> using namespace std; class Animal{ protected: string name; public: Animal(string name){ this->name = name; } virtual void makeSound(){ cout<<"Animal: "<<endl; } }; class Dog: public Animal{ string breed; public: Dog(string name, string breed):Animal(name){ this->breed = breed; } void makeSound(){ //Animal::makeSound(); to call makeSound function in Animal class cout<<"Dog makes Sound."<<endl; } void display(){ cout<<"Dog Name: "<<name<<endl; cout<<"Dog Breed: "<<breed<<endl; } }; class Cat: public Animal{ string color; public: Cat(string name, string color):Animal(name){ this->color = color; } void makeSound(){ cout<<"Cat makes Sound."<<endl; } void display(){ cout<<"Cat Name: "<<name<<endl; cout<<"Cat Color "<<color<<endl; } }; int main() { Dog d("Doggy","Male"); d.display(); d.makeSound(); cout<<endl; Cat c("Catty","Brown"); c.display(); c.makeSound(); return 0; } //OUTPUT: Dog Name: Doggy Dog Breed: Male Dog makes Sound. Cat Name: Catty Cat Color Brown Cat makes Sound.
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter