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);
}
Related
I am having trouble finding the method to store instances of similar classes into a vector. I thought about using a base class, but I am not sure whether it will work. For example, using sports:
class player //base
{
std::string name;
int age;
player(std::string name, int age) name (name), age (age);
};
class soccerplayer: public player
{
float goal_per_game;
//etc
};
class basketballplayer: public player
{
float defensive_blocks;
float three_pointers_per_game;
//etc
};
class hockeyplayer: public player
{
//etc
};
std::vector<player> favoriteplayers;
favoriteplayers.push_back(player("Lionel Messi", 33));
I am not sure if there is a method to store the various instances of this class. If not, what workaround is possible?
You can use a vector of (smart) pointers to the base class vs. a vector of values.
std::vector<std::unique_ptr<player> > favoriteplayers;
favoriteplayers.emplace_back(new sockerplayer(...));
favoriteplayers.emplace_back(new hockeyplayer(...));
All items in the vector point to the player base-class, but they can actually be objects of derived classes.
I have a base class called "Customer", and 2 derived classes "RegularAccount" and "VipAccount".
Now, I want to implement a "promote" member method, that allows RegularAccount to be promoted to VipAccount if it meets some condition.
How can I do this in C++? I tried to call a constructor inside the "promote" method of RegularAccount but was unable to do so. Is there a way to copy all data from RegularAccount's object to VipAccount's object in a member function of RegularAccount?
Here is the structure of my RegularAccount class:
class RegularAccount : public Customer {
private:
public:
// Constructors && Destructors
RegularAccount();
RegularAccount(string id, string name, string address, string phoneNumber, int numberOfRental);
~RegularAccount();
// Member functions
void rentItem(const string itemName);
void returnItem(const string itemName);
void details();
void showRentalList();
void promote();
};
VipAccount class:
class VipAccount : public Customer {
private:
int rewardPoints;
int freeRentItemAwarded;
public:
// Constructors
VipAccount();
VipAccount(string id, string name, string address, string phoneNumber, int numberOfRental);
// Member functions
void rentItem(const string itemName);
void returnItem(const string itemName);
void checkRewardPoints();
void details();
void showRentalList();
void promote();
};
Derive VipAccount from RegularAccount, makes sense cause Vip should have everything Regular has. Write a constructor for VipAccount which accepts an instance of RegularAccount as argument. In constructor pass the argument to the base class RegularAccount. To promote ,create an instance of VipAccount with RegularAccount as argument, delete instance of RegularAccount afterwards
I'm new to C++ programming and I want to create two classes that have the exact same data members and functions. Is it possible to create two inherited classes that have the same data members/functions instead of making several duplicate methods for each class. I'm making a c++ game based on zork and I want to create two items, weapons and fruits. Both will take in a name as a string and a value as a double. Do I create the header file as below:
#ifndef ITEM_H_
#define ITEM_H_
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Item {
private:
string description;
string longDescription;
float value;
public:
Item (string description, float inValue);
Item (string description);
string getShortDescription();
string getLongDescription();
float getValue();
void setValue(float value);
};
class Weapon:: public Item{
};
class Fruit:: public Item {
};
#endif /*ITEM_H_*/
How would I no go about creating the methods without duplicating them?
Do nothing for now. Both Weapon and Fruit are Items and contain all the members and methods that Item does.
Sooner or later, you'll want to specialize behaviour of child classes and the implementation of the method in the base class won't do it (if it's sensible to have an implementation in the first place). This is where polymorphism comes in. You'll make the method in the base class virtual and override it in the derived class:
class Item {
public:
virtual void Use() = 0; // pure virtual method
};
class Weapon : public Item {
public:
virtual void Use() override
{
Fire();
}
private:
void Fire() { /* do something */ }
};
Now when you have a reference or a pointer to the base class and you call Use on it, it'll dispatch to the corresponding method in the derived class.
EDIT: There's no way around "duplicating" constructors. Each class needs at least one if it is ever to be instantiated. Since you declared Item (string description, float inValue); you need to define it as a member of Item, too:
Item (string description, float inValue)
: description(description) // <-- avoid duplicating names of members in parameter list,
, value(inValue) // it does the right thing, but hurts readability
{ /* body empty */ }
If you need to call the constructor of the derived class with same parameters, you need to define another constructor and forward the arguments to the constructor of base class:
Weapon::Weapon(string desc, float val)
: Item(desc, val) { }
In C++11, there's a shortcut - you can inherit constructors and the compiler will generate these forwarding constructors for you:
class Weapon : public Item {
using Item::Item;
};
There's (unfortunately, perhaps) no way to specify which constructors you want to inherit. It's all or nothing.
Hope that helps.
you need to call constructor of parent class to assign value to description; and longDescription; like
class Weapon:: public Item{
Weapon(string description, float inValue):Item(description,inValue){}
Weapon(string description):Item(description){}
};
I'm doing a Systems Programming homework.
I have to implement a university.
I have a Course class, with child classes ComputerScience courses class, PG courses class, and Elective courses class.
class Course
{
public:
virtual void teach();
virtual void reg(Student &s)=0;
std::string getName();
std::string getDepartment();
int getSemester();
int getMinGrade();
void addStudent(Student *s);
void removeStudent(Student *s);
protected:
std::string _department;
std::string _name;
int _semester;
int _minGrade;
std::vector<Student*> studentsList;
};
class CSCourse : public Course
{
public:
CSCourse();
CSCourse(std::string department, std::string name, int semester, int mingrade);
~CSCourse();
std::string getName();
std::string getDepartment();
int getSemester();
int getMinGrade();
void addStudent(Student *s);
void removeStudent(Student *s);
};
(PG courses and Elective courses child classes are the same)
In the functions in the Course class (which are not void, like getSemester and such..) I just do dynamic_cast to figure what type of course is it.
I am having this problem:
coursesVector is:
std::vector<Course*> coursesVector
and dp variable is a string containing either CS, PG or Elective. In the main, I do this:
if (dp == "CS")
{
CSCourse *csCourse = new CSCourse(dp, name, semester, minGrade);
coursesVector.push_back(csCourse);
}
it gives me "Cannot allocate object of abstract type CS Course".
Same goes for PG and Elective!
But, in my definiton of hte class, CS course is not abstract!
The CSCourse class is abstract.
You have declared a pure virtual function reg in Course, but not provided an implementation in CSCourse.
You compiler undoubtedly told you exactly this as well.
You're inheriting from an abstract class which is fine, but you are never implementing the pure virtual function that the base class defines.
Also you need a virtual destructor in your base class;)
Edit:
You're also doing other things that probably aren't necessary like redeclaring most of your derived class functions. I bet their implementation is the exact same as your base class?
You have not implemented the pure virtual function reg in your derived class:
virtual void reg(Student &s)=0;
So yes, your class is abstract.
The MergeSort runs through the vector given it and sorts via the fitness variable in my Citizen base class.
I want to use this in my SGA class to do the same with its SCitizen (which inherits the fitness variable?); but I'm unable to as the function requests a Citizen type.
Is it not possible to use the derived class for the same function, as the function only uses members of the base class that all classes will have? Or have I missed something in the setup of the classes?
Thanks for any help!
Sorry if this is repeat; I had a search on here and a google but came up with nothing - I wasn't even particularly sure what to google..
class Citizen
{
public:
double fitness;
Citizen();
};
class SCitizen : public Citizen
{
public:
std::string code;
SCitizen();
};
class GA
{
public:
std::vector<Citizen>* citizens;
void MergeSort(std::vector<Citizen>* list);
};
class SGA : public GA
{
public:
std::vector<SCitizen>* sCitizens;
void FitnessSort();
};
In order for the vector<T> to store Citizen values and all of it's sub-types you need to use a vector<Citizen*>. A vector<Citizen> can only store Citizen values and will slice off any sub-types which are used.