accessing protected inheritance - c++

I have to inherit protected members of base class and keep as protected in derived also.
For-example: base class has set() and get() protected and they should be protected in derived also.
The problem is as they are protected in derived so how to access.
Actually I was trying to do this:
#include<iostream>
#include<string>
using namespace std;
class Animal
{
protected:
string name;
void setName(string a){ name = a; }
void getName(){ cout << name << endl; }
};
class Cat :public Animal
{
public:
Cat(string dc){
setName(dc);
}
void printName(){ getName(); }
};
class Dog :public Animal
{
public:
Dog(string dc2){
setName(dc2);
}
void printName(){ getName(); }
};
int main()
{
Cat c("Mano");
c.printName();
Dog d("Tommy");
d.printName();
system("pause");
return 0;
}

Related

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))

How to access parent class function from derived class object in function over riding feature of C++

#include<iostream>
using namespace std;
class base
{
public: void getdata()
{
cout<<"in base"<<endl;
}
};
class derived:public base
{
public: void getdata()
{
cout<<"in derived"<<endl;
}
void base:: getdata()
};
int main()
{
derived d;
d.getdata();
return 0;
}
I'm getting error: "cannot declare member function 'base::getdata' within 'derived'"
I want to print the content of both functions from derived class objects
#include<iostream>
using namespace std;
class base
{
public:
base() {
}
void getData()
{
cout << "in base" << endl;
}
};
class derived :public base
{
public:
derived(base b) {
b.getData();
}
void getdata()
{
cout << "in derived" << endl;
}
};
int main()
{
base b;
derived d(b);
d.getdata();
return 0;
}
try this.

can't take member of base class from derived class. C++

I have 2 classes. Base - Animal and derived MyDog. I want to change the ageOfAnimal from the setAge method and then display changed value from derived class - MyDog
code:
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age) {
ageOfAnimal = age;
return ageOfAnimal;
}
};
class MyDog: public Animal
{
public:
void getInfo() {
cout << ageOfAnimal;
}
};
int main()
{
Animal *animal = new Animal;
animal->setAge(20);
MyDog myDog;
myDog.getInfo();
return 0;
}
Output: 0
Why can't I get 20? Is this even posible?
UPDATE
Thanks for answers but what i want is to change the state of ageOfAnimal property from different objects
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age) {
ageOfAnimal += age;
return ageOfAnimal;
}
}
class MyCat: public Animal
{
public:
void getInfo() {
cout << ageOfAnimal;
}
}
class MyDog: public Animal
{
public:
void getInfo() {
cout << endl << ageOfAnimal;
}
}
int main() {
myDog dog;
dog.getInfo();
MyCat cat;
cat.getInfo();
}
output:
20
20
but what i want to see it's 20 21. How to do this?
In the usage code there are two different objects: Animal * animal and MyDog myDog. Both objects have their own instances of ageOfAnimal: changing it in one object does not modify the second object (which is the expected behavior).
1. As MyDog is derived from Animal one could simply:
MyDog myDog;
myDog.setAge(20);
myDog.getInfo();
2. If you want ageOfAnimal to be global for all instances of Animal you should make it a static member.
UPDATE
To reflect the updated question: one can put getInfo into Animal class. Default age for cats and dogs can be then specified in the constructors.
Even better way would be to specify age in Animal ctor and use constructor initializer lists for MyDog and MyAnimal ctors.
class Animal
{
public:
int ageOfAnimal = 0;
int setAge(int age)
{
ageOfAnimal += age;
return ageOfAnimal;
}
void getInfo()
{
cout << endl << ageOfAnimal;
}
};
class MyCat : public Animal
{
public:
MyCat()
{
setAge(40);
}
};
class MyDog : public Animal
{
public:
MyDog()
{
setAge(20);
}
};
int main()
{
MyDog dog;
dog.getInfo();
MyCat cat;
cat.getInfo();
}
Output:
20
40
Or if you want age to be shared between all objects, make it a static member as suggested earlier:
class Animal
{
public:
static int ageOfAnimal;
int setAge(int age)
{
ageOfAnimal += age;
return ageOfAnimal;
}
void getInfo()
{
cout << ageOfAnimal;
}
};
// Initialize age
int Animal::ageOfAnimal = 0;

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.

C++ - Function of a class with different statements

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;
}