C++ - Function of a class with different statements - c++

i'm trying to make specifics statements on the same class function's.
there's an example of what i'm triying to make
#include <stdio.h>
class animal
{
public:
void Talk();
};
int main()
{
animal dog;
animal cat;
dog::Talk()
{
printf("Wof");
};
cat::Talk()
{
printf("Meow");
};
dog.Talk();
cat.Talk();
return 0;
}
I also try it with class inheritance, something like
#include <stdio.h>
class cat
{
public:
void Talk()
{
printf("Meow");
};
};
class dog
{
public:
void Talk()
{
printf("Wof");
}
};
class animal{};
int main()
{
animal Schnauzer: public dog;
animal Siamese: public cat;
Schnauzer.Talk();
Siamese.Talk();
return 0;
}
There's a way to do something like this?

It is a very basic thing to do in c++. You just have to know a little bit about inheritance. However, I am guessing that you are new to c++ and don't have much experience in using inheritance. So, I am giving you a simple solution using class inheritance below. Feel free to ask me if there is any confusion.
#include <iostream>
using namespace std;
class animal {
public:
virtual void Talk() {cout << "default animal talk" << endl;}
};
class dog: public animal {
public:
void Talk() {cout << "Wof" << endl;}
};
class cat: public animal {
public:
void Talk() {cout << "Meow" << endl;}
};
int main()
{
dog Schnauzer;
cat Siamese;
Schnauzer.Talk();
Siamese.Talk();
return 0;
}

Related

How can I get a specific object of specific type from list of objects in Cpp

I have an animal manager class that has a list of object that implement the IAnimal interface. It has a method that must get the cats only from that same list. How can i design my code in a better way to achieve this ?
Sample code below (in C++)
#include<iostream>
#include<list>
class IAnimal
{
void doSomeThing();
};
class Cat : public IAnimal
{
void doSomeThing()
{
std::cout << "Cat" << std::endl;
}
};
class Dog : public IAnimal
{
void doSomeThing()
{
std::cout << "Dog" << std::endl;
}
};
class AnimalManager
{
private:
std::list<IAnimal> animals;
public:
void manageCat()
{
//get cats from animals list.
}
};

Polymorphysm Derived and Base class with :: Operator

The following programm creates a simple base class(Animal) and some derived classes(Frog,Cat).
The question occurs when typing Cat:: or Animal:: (See Code and you'llunderstand the question better)
#include <iostream>
using namespace std;
class Animal
{
protected:
int ID=4;
public:
void getID();
};
void Animal::getID()
{
cout << Animal::ID << endl;
}
class Frog:public Animal
{
public:
Frog();
};
Frog::Frog()
{
Animal::ID = 1;
}
class Cat:public Animal
{
public:
Cat();
};
Cat::Cat()
{
Cat::ID = 500;
}
int main()
{
Frog frog;
Cat cat;
cat.getID();
frog.getID();
}
/*
500
1
is printed to the screen
Question:
in line 26 whether I type Animal::ID=1;or Frog::ID=1;
I get the same result.
So in the derived class Frog, Animal:: and Frog:: are the same thing??
When I type Animal:: the programm understands Frog:: ??
(Same goes with Cat obviously)
*/
PS: Is there a way to cout the base(Animal) Class ID from a derived class(ex. Frog)??
(ex. frog.getID().SomeOtherFunction and the ID 4(Of the animal class is printed))

Strategy pattern on C++

I'm trying port example of Strategy pattern from HeadFirst book from java to C++
#include "iostream" using namespace std;
class IFlyBehavior
{
public:
virtual void fly() = 0;
};
class FlyWithWings : public IFlyBehavior
{
public:
void fly() override
{
cout << "fly!";
}
};
class FlyNoWay : public IFlyBehavior
{
public:
void fly() override
{
cout << "no fly!";
}
};
class IQuackBehavior
{
public:
virtual void quack() = 0;
};
class Quack : public IQuackBehavior
{
public:
void quack() override
{
cout << "Quack!";
}
};
class Squeak : public IQuackBehavior
{
public:
void quack() override
{
cout << "Squeak!";
}
};
class MuteQuack : public IQuackBehavior
{
public:
void quack() override
{
cout << "Can't quack";
}
};
class Duck : public IFlyBehavior, IQuackBehavior
{
public:
FlyWithWings* fly_behavior;
Quack* quack_behavior;
void swim()
{
cout << "Swim!";
}
virtual void display() = 0;
void performQuack()
{
quack_behavior->quack();
}
void performFly()
{
fly_behavior->fly();
}
};
class MallardDuck : public Duck
{
public:
MallardDuck()
{
quack_behavior = new Quack();
fly_behavior = new FlyWithWings();
}
void display() override
{
cout << "Mallard!";
}
};
class RedheadDuck : public Duck
{
public:
void display() override
{
cout << "RedHead!";
}
};
class DecoyDuck : public Duck
{
public:
void display() override
{
cout << "DecoyDuck!";
}
};
class RubberDuck : Duck
{
public:
void display() override
{
cout << "RubberDuck!";
}
};
int main(int argc, char* argv[])
{
Duck* md = new MallardDuck;
md->performFly();
md->performFly();
return 0;
}
But i got error:
E0322 object of abstract class type "MallardDuck" is not allowed: Duck d:\Code\CODE\C++\Duck\Duck\Source.cpp 119
It's seems like compiler not see realized classes, why this happen? Any ideas about it? How I must do?
You cannot instantiate a MallardDuck, because a MallardDuck is a Duck which supposedly implements the IQuackBehavior interface but has failed to override void Quack(). Same for the flying behaviour.
I recommend that you do not try to "translate" Java to C++; they are completely different languages and should be treated as such. Here are some good books for learning the language you're actually using.
MallardDuck inherits Duck which inherits from the abstract classes IFlyBehavior and IQuackBehavior. But nowhere do you override the abstract functions from those abstract classes.
Instead you seem to have a weird mix inheritance with encapsulation.

How to run abstract class functions and the inherited functions?

I have just learnt about abstract class but I don't understand much. Is it possible to run abstract class functions and the inherited functions all at once?..
For example,
class Animals
{
public:
virtual void Display() = 0;
};
class Dog : public Animals
{
void Display()
{
cout << "This is Dog!" << endl;
};
class Cat : public Animals
{
void Display()
{
cout << "This is Cat!" << endl;
}
};
and I have another class called Zoo which will run the abstract function in class Animals
class Zoo : public Animals
{
Animals* animal;
animal->Display();
}
and the output I want is
This is Dog!
This is Cat!
When I run this, it has errors.. Is there any other ways to get this output? Thanks :)
First off, there's a syntax error:
class Animals
{
public:
virtual void Display() = 0;
};
class Dog : public Animals
{
void Display()
{
cout << "This is Dog!" << endl;
}
};
class Cat : public Animals
{
void Display()
{
cout << "This is Cat!" << endl;
}
};
Then if you want to create Dog and Cat instances you call new operators for these classes:
class Zoo : public Animals
{
void Display()
{
Animals* animal1;
animal1 = new Cat();
animal1->Display();
delete animal1;
Animals* animal2;
animal2 = new Dog();
animal2->Display();
delete animal2;
}
}
This should get your desired output.
animal->Display(); results in undefined behaviour because animal is not initialized, so first initialized it as
Cat my_cat;
Animals* animal = &my_cat;
animal->Display();
OR
Animals* animal = new Cat();
animal->Display();
....
delete animal;
Here is your Code, explanation is there in comments.
class Animals {
public:
virtual void Display() = 0;/*its a PVF , in every derived class it should be re-defined */
};
class Dog : public Animals {
void Display() {
cout << "This is Dog!" << endl;
};
};
class Cat : public Animals {
void Display() {
cout << "This is Cat!" << endl;
}
};
class Zoo : public Animals {
public :
void Display() {
#if 1
Dog my_dog;
Animals *animal = &my_dog; /** Display() of Animal class will be invoked bcz Display() is declared as virtual */
animal->Display();
#endif
#if 0
Animals* animal = new Cat(); /** Display() of Cat class eill be invoked */
animal->Display();
delete animal;
#endif
}
};
int main() {
Zoo my_zoo;
my_zoo.Display();
return 0;
}
I hope it helps you.
Is there a way to only call the base class and run the inherited classes too?
I think you want this:
#include <iostream>
#include <memory>
class Animals
{
public:
virtual void Display() = 0;
virtual ~Animals() = default;
};
class Dog : public Animals
{
void Display() override
{
std::cout << "This is Dog!" << std::endl;
}
};
class Cat : public Animals
{
void Display() override
{
std::cout << "This is Cat!" << std::endl;
}
};
class Goat : public Animals
{
void Display() override
{
std::cout << "This is Goat!" << std::endl;
}
};
int main()
{
Animals* animal = new Dog;
animal->Display();
delete animal; // delete at some point to avoid memory leak as Dog is allocated on the heap
Cat cat;
animal = &cat;
animal->Display(); // no delete needed, as Cat is allocated on the stack and will cleared when goes out of scope
std::unique_ptr<Animals> animal2 = std::make_unique<Goat>();
animal2->Display(); // no delete needed, Goat is allocated on the heap but will be cleared when goes out of scope
return 0;
}
https://ideone.com/yoVt4G
Threw std::unique_ptr in the mix for variety

Ambiguous workaround for multiinheritance?

I have a base class called animal, and a dog and a cat that inherit from Animal.
And a multiinheritance class called dogcat, that inherit from dog and cat,
in Animal i have method called sleep. When i want to use that method from dogcat, i get the error "DogCat::sleep" is ambiguous, i do understand the problem, but i read in a book that it should be possible, when you declare sleep as virtual - but it does not work.
Is this not possible is the book wrong or is there any workaround?
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}
You have the diamond problem
The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
So. Now you have two instances of A. What is it the solution? You have two:
Define sleep operation in one of subclases and call it:
class Cat :public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
void sleep()
{
Animal::sleep();
}
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->Cat::sleep();
}
Use virtual inheritance like says #Asesh answer. The problem is the common method eat(). You have to override it.
You should use virtual inheritance
class Animal
{
public:
Animal(){}
virtual void sleep()
{
cout << "zzzzzzzzz" << endl;
}
virtual void eat() = 0;
};
class Dog: virtual public Animal
{
protected:
Dog(){}
virtual void eat() override
{
cout << "eats dogfood" << endl;
}
};
class Cat : virtual public Animal
{
public:
Cat(){}
virtual void eat() override
{
cout << "eats catfood" << endl;
}
};
class DogCat : public Dog, public Cat
{
public:
DogCat(){}
using Dog::eat;
};
int main(int argc, char** argv) {
DogCat *DC = new DogCat();
DC->sleep();//Error
}