member function void readPayInfo() is redefined in class BonusEmployee. It now returns the value of the data member base pay plus the value of the data member bonus
class Employee
{
public:
//constructors here
void readPayInfo()
{cin >> basePay;}
private:
double basePay;
};
class BonusEmployee : public Employee
{
public:
//constructors here
void readPayInfo()
{cin >> basePay >> bonus;} // NULL!
private:
bonus;
};
how do I access basePay from parent class?
Since you've opted to make Employee::basePay private there is no way to directly access it from a subclass. I think you have two options:
Add a getter to Employee, or
Change the visibility of Employee::basePay to protected.
Related
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person {
private: char name[50];
int age;
public: void get_name() {
cout<<"Enter name"<<endl;
gets(name);
}
void put_name() {
cout<<"Name : ";
puts(name);
cout<<endl;
}
void get_age() {
cout<<"Enter age"<<endl;
cin>>age;
}
void put_age() {
cout<<"Age : "<<age<<endl;
}
};
class student : public person {
private : int roll;
public : void get_roll() {
cout<<"Enter roll"<<endl;
cin>>roll;
}
void put_roll() {
cout<<"Roll : "<<roll<<endl;
}
};
int main() {
student A;
A.get_name();
A.get_roll();
A.get_age();
A.put_name();
A.put_age();
A.put_roll();
getch();
clrscr();
return 0;
}
There are following queries:
If private members of class person are not inherited in class student then how can an instance of class student store values in them?
Shouldn't the variables just not exist in the class student?
Note: I am using old compiler for a college project.
If a Student wants to access the Person fields, the simplest way is make them protected, so they can be accessed by all derived class of Person.
Another way is use public getter/setter methods (you can even set them protected, but at this point is better use protected fields), in this way all classes can see and set the fields of Person.
Indeed, even if a variable is declared private in the base class, it exist in the derived class ( a Student is in any case a Person, so the field in Person must be initialized) but it can't be reached by her.
A is an object of type Student and it can access protected or public member function and data member of class Person. Now answer to your query, you are trying to use the function of class Person using object of class Student which is possible (as inherited publicly) and since the function and those private data member are part of same class (Person), so those function can use the variable.
Note : private, public and protected member have no effect within same class.
I am trying to access the get_workhour() function. How do I do this?
Does polymorphism only works for overriden functions?
The manager class has a special function get_workhour() which is not present in base class employee. How can we call that function when both class's objects are stored in a vector of employee type, using polymorphism?
#include <iostream>
#include <vector>
using namespace std;
class employee{
protected:
string name;
double pay;
public:
employee(){
name="xyz";
pay =0.0;
}
employee(string name,double pay) {
this->name = name;
this->pay = pay;
}
string get_name(){
return name;
}
virtual double get_pay(){
return pay;
}
};
class manager:public employee{
protected:
int workhour;
public:
manager(){
workhour = 0;
}
manager(string name,double pay,int workhour):
employee(name,pay){
this->workhour = workhour;
}
virtual int get_workhour(){
return workhour;
}
virtual double get_pay() {
return pay + 100;
}
};
int main()
{
employee emp1("vivek",1500);
manager m1("william",1300,10);
vector<employee*> emps;
emps.push_back(&m1);
emps.push_back(&emp1);
cout<<"Name of manager is : "<<emps[0]->get_name()<<endl;
cout<<"Pay for the manager is :"<<emps[0]->get_pay()<<endl;
cout<<"Workhour of manager is : "<<ptr->get_workhour()<<endl;
cout<<"Name of employee is : "<<emps[1]->get_name()<<endl;
cout<<"Pay for the employee is : "<<emps[1]->get_pay()<<endl;
return 0;
}
You can't and you shouldn't. If you want all Derived to be interacted with via Base's interface, then everything you want to do to a Derived must be represented by Base's interface.
In general C++ (and most object oriented languages) try to discourage downcasting (casting a base type to a derived type). If you absolutely need to you can use a dynamic_cast to cast an employee to a manager. But do this only if you are sure that your pointer actually refers to a manager. Without a cast you only have access to the base type's interface.
You can use the dynamic_cast as follows:
manager* m = dynamic_cast<manager*>(emps[0]);
One option is to have an empty method for get_workhour() in the base class and just have the manager class override it.
I am new into classes, and I have been trying to create this simple class code but every time I get an error. It works fine when I don't use the access specifier private, but I want to practice how to use private. Could you please tell me what's wrong?
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;
int ID;
public:
void setName(string);
string getName();
void setID(int);
int getID();
};
void Student::setName(string n)
{
name=n;
}
string Student::getName()
{
cout<<name;
return name;
}
void Student::setID(int i)
{
ID=i;
}
int Student::getID()
{
cout<<ID;
return ID;
}
int main ()
{
Student S;
cout<<"Enter Student name: ";
cin>>S.name;
cout<<"Enter students ID: ";
cin>>S.ID;
cout<<"The student's name is "<< S.name<<endl;
cout<<"The student's ID is "<< S.ID<<endl;
return 0;
}
In your main function you are trying to access the name and IDmember of your class. Which are private... Since you are outside the scope of the class Student, the compiler shouts at you.
You should do this (as you've implemented setters and getters):
int ID(0);
std::string name;
std::cin >> name;
S.setName(name);
std::cin >> ID;
S.setID(ID);
You have to access your private fields using setters/getters to set or retrieve their values, you can't use them with the class dot notation because they're private and you can access to them only using public methods
What's wrong : You are accessing private members without using a class member function (outside of the class scope).
Private members are useful when you want to protect a value from uncontrolled access. Like when the modification of a value must undergo a certain verification (which would be implemented in class functions).
In your code, you made sure name and ID are private, which means both can only be accessed using class functions (like the constructor or the getter and setter).
If you wanted, you could create a class named classroom (which would contain many students, stored in a vector).
In that class, you could make sure than when a student is added, it's ID is automatically generated and does not equal any other ID. In that case, it would be important to put the student vector private since it would require some sort of validation.
class Student
{
private: // anything that wants to access members below
// this must be defined as a class member, or the equivalent
string name;
int ID;
public:
void setName(string); // can access private members
string getName(); // can access private members.... should be const
void setID(int); // can access private members
int getID(); // can access private members, should be const
};
I am currently busy with inheritance and I'm working with a base class called Vehicle and a subclass called Truck. The subclass is inheriting from the base class. I am managing with the inheritance of the public members of Vehicle but can't access the private member called top_speed in the function void describe() const.
I know that one can do this in code (provided below) to access from the base class but it seems like I'm missing something. In the comment section in the code my question is stated more clearly for you.
void Truck::describe() const : Vehicle()
/*I know this is a way-> : Vehicle()
to inherit from the Vehicle class but how
can I inherit the top_speed private member of Vehicle in the
void describe() const function of the Truck class?*/
{
cout << "This is a Truck with top speed of" << top_speed <<
"and load capacity of " << load_capacity << endl;
}
Where in my Vehicle class it is:
class Vehicle
{
private:
double top_speed;
public:
//other public members
void describe() const;
};
and in the Truck class it is this:
class Truck: public Vehicle
{
private:
double load_capacity;
public:
//other public members
void describe() const;
};
To be even more clear I am getting this error:
error: 'double Vehicle::top_speed' is private
What can I do in the void Truck::describe() const function to fix it?
The cleanest way is to have a public accessor function in the base class, returning a copy of the current value. The last thing you want to do is expose private data to subclasses(*).
double Vehicle::getTopSpeed( void ) const {
return this->top_speed;
}
and then use it in the Truck class like this:
void Truck::describe( void ) const {
cout << "This truck's top speed is " << this->getTopSpeed() << endl;
}
(Yeah, I know, "this->" is superfluous but it's used for illustrating that it's accessing members/methods of the class)
(*) Because, really, if you're going to expose data members of superclass(es), then why would you use private/protected/public at all?
Simply declare top_speedas protected
class Vehicle
{
protected:
double top_speed;
public:
//other public members
void describe() const;
};
If you can't modify the base class then you just can't access it.
Joe_B said
I am not allowed to make changes to the Vehicle class given
Then, there is still a very hugly way to access private members of a class you cannot modify (3rd party for instance).
If verhicle.h (let's say that's the class you have no way to modify) is:
class Vehicle
{
private:
double top_speed;
public:
Vehicle( double _top_speed ) : top_speed( _top_speed ) {}
};
and truck.h is:
#include "vehicle.h"
class Truck: public Vehicle
{
private:
double load_capacity;
public:
Truck( double _load_capacity, double _top_speed ) :
Vehicle( _top_speed ),
load_capacity( _load_capacity )
{}
void describe() const
{
std::cout << "This is a Truck with top speed of "
<< top_speed
<< " and load capacity of "
<< load_capacity
<< std::endl;
}
};
Compiling this will give the error
Vehicle::top_speed is private
Then, let's do this:
In trunk.h, change #include "vehicle.h" into:
#define private friend class Truck; private
#include "vehicle.h"
#undef private
Without modifying vehicle.h, you made Truck class be a friend of Vehicle class, and it can now access Vehicle's private members!
Please, do not comment that it is a very bad idea to do this as it breaks all C++ rules....I know, you only want to do this if:
You really need to access a private member and have no alternative
There's really no way you cannot modify vehicle.h
You definitely know what you are doing, because if the guy who coded Vehicle made this attribute private, it was most likely because he did not want you to access it...
I used to do this when customizing behaviour of some MFC classes in the past...I definitely could not change the header files defining them....
i have two c++ classes, one which inherits an abstract base class for a student database.
The base class is the record which contains all the student information (name, id vector of courses & marks):
class student{
protected:
string fName,sName;
int id;
vector<string> cname;
vector<double> cmark;
public:
virtual ~student();
virtual void addClass(string name, double mark)=0;
};
I Need to be able to access the vector cname and cmark in the addCourse function in the below class
class degree : public student{
public:
degree(string f, string s, int i){
this->setName(f,s);
this->setID(i);
}
~degree();
void AddCourse(string name, int mark){
}
I dont know how to do this without making a set function in the base class like i have done with the degree constructor.
I could just make a set function in the base class but i would rather some method of initializing the inherited elements without using functions, just to make the code less messy, is this possible? i thought about using this->cname but that gave me errors.
I Need to be able to access the vector cname and cmark in the addCourse function in the below class
Just access them, they are protected, so derived classes have access:
void AddCourse(string name, int mark){
cname.push_back(name);
cmark.push_back(mark);
}