Why constructor of Ibuy class not called - c++

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

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.

Linkedlist contain differnt classes of object in cpp

How we can create a linked list which can contain a different class of object. for example, if a person is base class and it has student and teacher as a derived class so How I can create a linked list which can contain both classes of objects like student also and teacher also.
LinkedList example for understanding my problem:
head->studentobject->teacherobject->studentobject->teacherobject... so on
Try it here. A std::list is a linked list . .
#include <list>
#include <iostream>
#include <memory>
using namespace std;
class Person {
public:
virtual ~Person() = default;
virtual void talk()=0;
};
class Student : public Person {
public:
void talk() {
cout << "Teach me!\n";
}
};
class Teacher : public Person {
public:
void talk() {
cout << "Listen!\n";
}
};
int main() {
list<std::unique_ptr<Person>> people;
people.push_back(unique_ptr<Person>(new Student()));
people.push_back(unique_ptr<Person>(new Teacher()));
for (auto& p : people) {
p->talk();
}
return 0;
}
Usually you would create a base class and derived classes:
#include <iostream>
#include <list>
#include <memory>
class Base {
public:
virtual ~Base() = default;
virtual void printClass() = 0;
};
class Student : public Base {
public:
virtual void printClass() { std::cout << "Student\n"; }
};
class Teacher : public Base {
public:
virtual void printClass() { std::cout << "Teacher\n"; }
};
class Node {
public:
void setTeacher() {
data = std::make_unique<Teacher>();
}
void setStudent() {
data = std::make_unique<Student>();
}
void printClass() { data->printClass(); }
private:
std::unique_ptr<Base> data;
};
int main() {
std::list<Node> l;
l.push_back({});
l.front().setTeacher();
l.front().printClass();
}
Use some kind of pointer to store the references. You can use raw pointers, references, smart pointers, ...
Here you can read when to use virtual destructors

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;

accessing protected inheritance

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