Why am I not able to set the class name and inherit it and display in another class like the code given below? What am I doing wrong?
#include<iostream>
#include<string>
using namespace std;
class dealer
{
string name;
public:
dealer(){};
dealer(string n): name(n){};
~dealer(){};
//void setname(string n){name = n;};
void display(){cout<<name<<endl;}
};
class car : public dealer
{
public:
dealer b;
car(){};
//car(string n): dealer(n){};
~car(){};
void display(){dealer::display();}
};
int main ()
{
dealer a("ABC");
car c;
c.display();
}
It is working properly. You initialize c with the car default constructor which implicitly call the dealer default constructor which doesn't do anything. name remains an empty string, which is what is printed out. To solve this problem:
//car(string n): dealer(n){}; <--- uncomment
car c("ABC"); //initialize like this
From comment: Your inheritance hierarchy is incorrect. car has-a dealer, but isn't a dealer. A better design system would look like this:
class dealer { ... };
class car
{
dealer &d;
car(dealer &nd): d(nd) { };
void display(){ d.display();}
}
Related
I'm new to c++. I'm trying to complete a c++ assignment involving Inheritance where the Car class must contain these methods setGarageSpaces function to set garage space to 2, and setNumWheels function to set wheels to 4.
I'm creating a no parameter constructor and inheriting from the base class Vehicle then using the set functions but I'm getting an error.
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle();
void setGarageSpaces(size_t spaces)
{
garage_spaces_ = spaces;
}
void setNumWheels(size_t wheels)
{
num_wheels_ = wheels;
}
Vehicle(std::string name, std::string manufacturer, double top_speed, double weight, double mpg, double curr_gas_amt);
protected:
size_t garage_spaces_; // number of garage slots this vehicle takes up
size_t num_wheels_;
};
class Car : public Vehicle { //Car derived class inherits from base class Vehicle
public:
Car(){ //no para constructor
setGarageSpaces(2);
setNumWheels(4);
}
Car(std::string name, std::string manufacturer, double top_speed, double weight, double mpg);
};
int main() {
Car c;
}
public:
Vehicle();
Default Constructor for Vehicle is not implemented, you may want to do :
public:
Vehicle() {}
When you expect proper construction, you can remove the default constructor from class, forcing user to impl. the proper constructor call.
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;
}
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))
(Beginner in OOP.)
I have a person class which stores the name of the person. In a derived class instance printer, I need that name so that I can do further tasks with it.
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer: public Person{
public:
void print(){
printf("%s", this->name.c_str());
}
};
int main() {
Person one;
one.name = "john";
Printer printer;
printer.print();
return 0;
}
What am I not doing to have printer see the data in one ? I'd be having several printer-like objects so storing "john" only once is the goal here.
You have made the member vars public but this is not the right approach for the OOP, the right one is to have them under private access specifier and use setters and getters.
Now to your mistakes:
You use void for main but with c++ you can only use int for it.
You use std::string as an argument for printf but it can't accept it. (I passed the c_string of the std::string to correct that).
You use an object, from the parent class, and give it a name then use another object, from the driven one, to print the name of the first one. (I used only one)
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer: public Person{
public:
void print(){
printf("%s",this-> name.c_str());
}
};
int main() {
Printer printer;
printer.name = "name";
printer.print();
}
After your comments, I have updated Printer class to do your inttent
#include <iostream>
#include <string>
class Person{
public:
std::string name;
};
class Printer{
public:
void print(const Person& person ){
printf("%s", person.name.c_str());
}
};
int main() {
Person one;
one.name = "name";
Printer printer;
printer.print(one);
}
when we say "a member declated as protected is accessible to any class imediately derived from it" what does this mean.
in the follwing example get_number function can be accessible by the result class , as per the statement it sould only be accessile to test class.
class student
{
protected:
int roll_number;
public:
void get_number(int){ cout<< "hello"; }
void put_number(void) {cout<< "hello"; }
};
class test : public student
{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float) {cout<< "hello"; roll_number = 10; }
void put_marks(void) {cout<< "hello"; cout << "roll_number = " << roll_number ; }
};
class result : public test
{
float total;
public:
void display(){cout<< "hello"; roll_number = 10; }
};
int main()
{
result student;
student.get_marks(2.2, 2.2);
student.put_marks();
return 0;
}
i changed the code as per the first statement the protected variable roll_number not be accessible upto the result class ?
You have declared get_number as public so all classes can see it.
If you want class result to not have direct access to data member roll_number you need to change the inheritance access of class test to protected:
class test : protected student
{
};
For more information, see The C++ FAQ Lite: Public and Private Inheritance. Changing how class test inherits from class student also affects how data members in class student are accessed by classes derived from class test.
An alternative to inheritance is for class test to contain a private pointer to an instance of class student, as long as class student is not an abstract class.