educative.io

https://www.educative.io/courses/learn-cpp-from-scratch/qV6vxXyNXO0

how to display ouptut in dvc++ for this code below :slight_smile:
#include
#include
using namespace std;

class Animal
{
protected:
//define members here
string name;
int age;
public:
//define members here
void set_data (int a, string b)
{
//define here
a= age;
b= name;
}
};
//define derived class named “Zebra” here
class Zebra:public Animal
{
public:

string message_zebra(string str)
{
		//define here
	str= "The zebra named "+name+" is "+ to_string(age) +"years old. The zebra comes from Africa.";
    return str;
}

};

//define derived class named “Dolphin” here

class Dolphin: public Animal
{
public:

string message_dolphin(string str)
{
    //define here
    str= "The dolphin named "+name+" is "+ to_string(age) +"years old. The dolphin comes from New Zeland.";
    return str;
}

};


Course: Learn C++ from Scratch - Free Interactive Course
Lesson: Exercise 3: Displaying Message Using Inheritance - Learn C++ from Scratch

Hi @scott_mickel
Thanks for contacting us. You can execute the above code by creating an instance of the class and then you can access the attributes and functions of the class.
Here you go:
```
#include <iostream>
#include <string>
using namespace std;

class Animal
{
protected:
int age;
string name;
public:
void set_data (int a, string b)
{

   age = a;
   name = b;
}

};

class Zebra:public Animal
{
public:

string message_zebra(string str)
{
		
        
		str = "The zebra named " + name + " is " + to_string(age) + " years old. The zebra comes from Africa.";
		return str;
}

};

class Dolphin: public Animal
{
public:

string message_dolphin(string str)
{
	str = "The dolphin named " + name + " is " + to_string(age) + " years old. The dolphin comes from New Zeland.";
  return str;
}

};

int main() {
// Create an object of MyClass

Zebra myZebra;
Dolphin myDolphin;

// Access attributes and set values
myZebra.set_data(15,“Dolly”);
myDolphin.set_data(25,“Jolly”);

// Print attribute values
cout<<myZebra.message_zebra("")<<endl;
cout<<myDolphin.message_dolphin("");

return 0;
}

I hope it helps. Happy Learning :)
1 Like