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))
Related
I came across the scenario where the constructor of base class IBuy is not called. Ideally it should be called as it is inherited in shop class and shop is inherited in mall class. Please resolve this query
#include <iostream>
using namespace std;
class Ibuy
{
public:
Ibuy(){"Ibuy default constructor \n";}
virtual void nameItem()=0;
};
class shop: public Ibuy
{
public:
shop()
{
cout<<"def constructor of shop class \n";
}
void shopName(string name)
{
cout<<"Name of shop is: "<<name<<endl;
}
};
class mall:public shop
{
public:
void nameItem()
{
cout<<"Item name... \n ";
}
};
int main()
{
cout<<"Abstract class \n";
mall mObj;
mObj.shopName("vishal mart");
mObj.nameItem();
return 0;
}
I have this hierarchy of classes :
B1 B2
\ /
C
How should I make an array of pointers in C++ , in which I can store objects from all the classes?
This is what I tried and is not working:
#include <iostream>
using namespace std;
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};
class MyChildClass: public MyClass, public MyOtherClass {
};
int main() {
MyChildClass **v=new MyChildClass*[3];
v[1]=new MyClass();
v[2]=new MyOtherClass();
v[1]->myFunction();
return 0;
}
First of all you have to understand, that you can store only pointer to child into pointer to parent and only in that order. Secondly, You may want to use std::variant, so your code will looks like this
#include <iostream>
#include <variant>
using namespace std;
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};
class MyChildClass: public MyClass, public MyOtherClass {
};
int main() {
std::variant<MyClass*, MyOtherClass*> v[3];
v[1]=new MyClass();
v[2]=new MyOtherClass();
std::get<MyClass*>(v[1])->myFunction();
std::get<MyOtherClass*>(v[2])->myOtherFunction();
return 0;
}
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;
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
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;
}