I dont understand what to do in the read() method - c++

This is my c++ homework and i dont really get what they meant by setting the values in the method read().
Question: Create a base class called Athlete that contains 2 member variables for attributes common to all professional athletes: name and annual salary. It should also contain pure virtual method, read(). The method read() is called to read data from the user for setting the values of the attributes.
Here is my header file
#ifndef ATHLETE_H
#define ATHLETE_H
#include <string>
using namespace std;
class Athlete
{
public:
Athlete();
~Athlete();
void setName(string name);
string getName() const;
void setSalary(double salary);
double getSalary() const;
virtual void display() const;
virtual void read(string name, double salary) const;
private:
string name;
double salary;
};
#endif
And my cpp
#include "Athlete.h"
#include <iostream>
Athlete::Athlete() {}
Athlete::~Athlete() {}
string Athlete::getName() const { return this->name; }
void Athlete::setName(string name) {
this->name = name;
}
double Athlete::getSalary() const {
return this->salary;
}
void Athlete::setSalary(double salary) {
this->salary = salary;
}
void Athlete::read(string name, double salary) const {
Athlete* temp = new Athlete();
temp->setName(name);
temp->setSalary(salary);
}
void Athlete::display() const {
cout << "Name: " << this->getName() << endl;
cout << "Salary: " << this->getSalary() << endl;
}
I tried to use the setter methods in read but theres an error.

I think you misread the question. It says that the read() method should read the data from the user. Usually it means read from the standard input. Afterwards, the method should set the values of the attributes for this specific athlete. Meaning, that the entered values relate to this specific object. Not for something new and temporary.
Pulling everything together is may look like the following:
void Athlete::read()
{
string name;
double salary;
std::cout << "Please enter the athlete name:";
std::cin >> name;
std::cout << "Please enter the athlete salary:";
std::cin >> salary;
setName(name);
setSalary(salary);
}

The thing you've missed is that read is supposed to be a pure virtual function. This means that you should not actually implement it, instead you should declare it as:
virtual void read(string name, double salary) = 0;
This means that the Athlete class cannot actually be instantiated (it's called an absract class), instead it will be used as a base class and derived classes would be required to override the read method. If they don't override the method they will themselves be abstract and cannot be instantiated.
You are not required to implement the read method as a method of Athlete once you declared it as pure virtual. It only need to be implemented as a method in the derived class(es).
Also as the method in the derived class is supposed to modify the object the method cannot be const declared (as shown above).

Related

How to serialize the data in Binary I/O?

Here is my C++ class and code snip. Data does not gets properly loaded in objects which gives me memory access error. What can i do?
Base Class
using namespace std;
class Person;
class Person
{protected:
int ID;
string name;
string address;
long int phone;
string dob;
//char pass[25];
std::string pass;
public:
Person();
Person(int ID,string name, string address, long int phone, string dob, string pass) :
ID(ID),name(name), phone(phone),address(address),dob(dob),pass(pass)
{};
//COPY CONSTRUCTOR MUST BE CREATED INORDER TO MAKE VECTOR WORK
Person(const Person&);
virtual void showDetails() const = 0;
//MAKING THIS CLASS AN ABSTRACT CLASS
//BUNCH OF GETTERS
int getID() const;
string getName() const;
string getAddress() const;
long int getPhone() const;
string getDob() const;
string getPass() const;
void setPass(string a);
};
Here is the derived class from it :
#include<iostream>
#include"Person.h"
#ifndef CUSTOMER_H
#define CUSTOMER_H
class Customer :public Person {
private:
float balance;
protected:
public:
Customer() :Person(), balance(0) {}
Customer(int ID,std::string name,std::string address,long int phone, string dob,std::string
pass,float balance):
Person(ID,name,address,phone,dob,pass),balance(balance){};
//Customer(int ID, const char* name, const char* address, long int phone, string dob, const
char* pass, float balance) :
// Person(ID, name, address, phone, dob, pass), balance(balance) {};
//COPY CONSTRUCTOR MUST BE PROVIDED, ELSE VECTOR WONT WORK
Customer(const Customer& other) :Person(other) {
this->balance = other.balance;
}
float getBalance() const;
void showDetails() const;
// void setValues();
void deposit(float);
void withdraw(float);
};
#endif
Im having issues that data is not copied from file into vector properly : Here is the implementation of file handling:
void Controller::displayCustomers()
{
vector<Customer> custVector;
Customer cust;
fstream fin("customer.txt", ios::binary | ios::in);
while (fin.read(reinterpret_cast<char*>(&cust), sizeof(cust)));
{
custVector.push_back(cust);
}
fin.close();
cout << "ID\tNAME\tADDRESS\tPHONE\t\tDOB\tPASSWORD\tBALANCE" << endl;
for (vector<Customer>::iterator itr = custVector.begin();
itr != custVector.end(); ++itr)
{
cout << itr->getID() << "\t" << itr->getName() << "\t" << itr->getAddress() << "\t"
<< itr->getPhone() << "\t" << itr->getDob() << "\t" << itr->getPass() <<
"\t" << itr->getBalance() << endl;
}
cout << endl;
}
The constuctors runs more than data members and gives a memory access error when vector members are accessed. What should be the best practice.?
You cannot use binary I/O to directly read or write objects containing std::string. So this code is wrong.
fin.read(reinterpret_cast<char*>(&cust), sizeof(cust))
Best practise depends entirely on what you are trying to achieve. Must you use binary I/O? Can you change the class? Are you trying to get indexed access on your file?
For better advice you need to state what you are actually trying to achieve. At the moment all anyone can say it that this code is wrong.
It's possible that you just chose binary I/O because you throught it would be easiest. If that's the case then you should probably abandon it and switch to text I/O. But I'm not going to give advice based on speculation. You need to say what you are trying to do here.
ADDENDUM
I noticed this is your code
//char pass[25];
std::string pass;
If you followed through with that, and replaced all the strings in your classes then you might find that your reading and writing code started working. This is what's known as a plain old data (POD) class. POD classes can be read and written in the way you are trying to.
Note you would also have to remove any virtual functions as well, so maybe a POD class isn't such a good idea.

C++: how to make getters and setters work with an empty constructor

First of all, I have only learned a little bit of Java before. It's been only a few days since I started getting friendly with C++ so please don't take this question so basic and please don't degrade my question.
I made a simple source code as follows:
#include <iostream>
using namespace std;
class Car {
public:
void setBrand(string name);
void setPrice(double price);
string getBrand();
double getPrice();
Car();
Car(string name);
Car(string name, double price);
private:
string name;
double price;
};
Car::Car() {
}
Car::Car(string name) {
name = name;
}
Car::Car(string name, double price) {
name = name;
price = price;
}
void Car::setBrand(string name) {
name = name;
}
void Car::setPrice(double price) {
price = price;
}
string Car::getBrand(void) {
return name;
}
double Car::getPrice(void) {
return price;
}
int main() {
Car car;
car.setBrand("Nissan");
car.setPrice(30000);
cout << "Brand: " << car.getBrand() << endl;
cout << "Price: " << car.getPrice() << endl;
return 0;
}
I wanted to make a code that creates an empty instance of a class called Car, set the field values later and print them out on the console.
The code did not make any errors during the compile, but the result I see was totally different from what I expected. It didn't show the brand name and the price was looking even weird, as follows.
Brand:
Price: 6.95322e-310
Somebody help me out! Thank you very much indeed in advance.
The problem you have is that you override the member names with function parameters. You can use this-> to make it explicit or name the member differently.
For example:
void Car::setBrand(string name) {
this->name = name;
}
Or:
void Car::setBrand(string new_name) {
name = new_name;
}
In your constructor and setters, you make no differentiation between the local parameter and the class member.
name = name;
Both the function parameter and the class member are called name. Currently the compiler is assigning the parameter value to itself, and not affecting the class member at all. This is because the function parameter is in a more immediate scope.
Possible solutions:
Specify this when referring to the class member: this->name = name;.
Rename the function parameter: name = _name;.
For the constructor, use initializer lists:
Car::Car(string name, double price)
: name(name)
, price(price)
{ }
There's too much wrong with your code to describe it in prose, so let me present a fixed implementation, and I leave it to you to spot the difference:
#include <string>
class Car
{
private:
static constexpr double kNoPrice = -1.0;
static constexpr const char* kNoName = "[no name]";
public:
// Main constructor: constructs a car with the given name and price.
Car(std::string name, double price)
: name_(std::move(name))
, price_(price)
{}
// Convenience constructors:
Car() : Car(kNoName, kNoPrice) {}
Car(std::string name) : Car(std::move(name), kNoPrice) {}
// Accessors:
const std::string& getBrand() const { return name_; }
void setBrand(std::string name) { name_ = std::move(name); }
double getPrice() const { return price_; }
void setPrice(double price) { price_ = price; }
private:
std::string name;
double price;
};
Some random notes, in no particular order:
Use correct names. It's std::string, not string, mate or buddy. Never ever be abusing namespace std.
Include headers for external names that you need.
Reading uninitialized values is undefined behaviour, so none of your constructors should leave fields uninitialized (like price_).
Give private members consistent names (e.g. foo_ in my example).
Accessors should be const-correct.
Convenience constructors should delegate to one single work-horse constructor.
Pick sensible defaults for initial values of defaulted fields and make them discoverable.
Use move semantics when taking ownership of dynamically managed data (strings, dynamic containers, etc.).

How do I change different elements of the same with same member function?

I am new to C++ and am not able to find the answer. This is the code I want to write:
#include <iostream>
using namespace std;
class Employee{
private:
string name;
string gender;
public:
void display();
void update(string);
Employee(string a, string b){
name = a;
gender = b;
};
~Employee(){};
};
void Employee::display(void){
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
}
void Employee::update(string a){
/*
a function that updates either the
name element or gender element
based on which element it is used by.
*/
}
int main(){
Employee employee1 ("Joe","Male");
Employee employee2 ("Jon","Male");
employee1.display();
employee2.display();
employee1.name.update("Mary"); // This is what I want to do: Same function
employee2.gender.update("Female"); // for different elements of same type
employee1.display();
employee2.display();
return 0;
}
How do I go about doing this?
I thought about function overloading but both elements are of the same type. I do not want to pass any extra values and make the code look crappy. Any ideas? Thank you.
Use setters and getters like this:
void Employee::setName(const string &a) {
this->_name = a; // validate or whatever you need to do
}
const string &Employee::name() const {
return this->_name;
}
void Employee::setGender(const string &a) {
// ....
}
Usage as one would expect
employee1.setName("Mary");
employee2.setGender("Female");
The basic_string class already implements a 'setter':
employee1.name.assign("Mary");
employee2.gender.assign("Female");
If you want to access name and gender like you have written in your question, you need to make both public, as #sop correctly pointed out.

Having trouble with composition C++

I'm trying to declare a member-object in a class in C++, like so:
class bankAccount
{
public:
bankAccount(int, int, int, string, int);
private:
bankBranch homeBranch;
};
bankAccount::bankAccount(int accountNumber, int accountBalance, int homebsbnumber,
string homeaddress, int homepostcode) : homeBranch(homebsbnumber, homeaddress, homepostcode) {}
class bankBranch
{
public:
/** bankBranch(const int& bsbNumber, const string& address, const int& postCode)
{
this->bsbNumber = bsbNumber;
this->address = address;
this->postCode = postCode;
}; **/
bankBranch(int, string, int);
void setPostCode()
{
cout << "Please type in the postal code of your bankBranch: " << endl;
cin >> postCode;
}
void setBsbNumber()
{
cout << "Please type in the BSB Number of your bankBranch: " << endl;
cin >> bsbNumber;
}
void setAddress()
{
cout << "Please type in the address of your bankBranch " << endl;
cin >> address;
}
// Return methods for bsb number, address and post code
int getBsbNumber()
{
return this->bsbNumber;
}
string getAddress()
{
return this->address;
}
int getPostCode()
{
return this->postCode;
}
private:
int bsbNumber;
string address;
int postCode;
};
bankBranch::bankBranch(int bsbnum, string bankaddress, int bankpostcode) {
bsbNumber = bsbnum;
address = bankaddress;
postCode = bankpostcode;
}
I am getting these erros:
sytax error : missing ';' before identifier 'homeBranch'
missing type specifier - int assumed (confused because it is obviously an object?)
'bankAccount' : illegal member initialization: 'homebranch' is not a base or member
I have tried many things and cannot seem to fix these errors, could anyone provide me with some insight on how to properly add the member-object bankBranch homeBranch to the class bankAccount? And how to properly use initialization lists.
C++ is an (almost) single-pass, top-to-bottom language. The compiler must see the declaration of a thing before it's used. Move the definition of class bankBranch above that of class bankAccount.
Just put the definition of bankBranch before your class bankAccount. See a live example here: http://ideone.com/LuMzBt
Don't forget to include string and iostream in you code.
It isn't recognising your BankBranch as a data type.
Removing the comments might help you.
And defining the bankbranch class before the other class might also help.
Also, it is good practice to define classes in separate header files, then include them separately in all the files.
Just an advice!

Polymorphism constructor function

My code compiles fine but I am having issues with a particular section not displaying the correct output.
Here is my base class
class Item
{
protected:
//int count;
string model_name;
int item_number;
public:
Item();
Item(string name, int number);
string getName(){return model_name;}
int getNumber(){return item_number;}
and here is my derived class:
class Bed : public Item
{
private:
string frame;
string frameColour;
string mattress;
public:
Bed();
Bed(int number, string name, string frm, string fclr, string mtres);
Function definitions:
Bed::Bed(int number, string name, string frm, string fclr, string mtres)
{
model_name=name;
item_number=number;
frame=frm;
frameColour=fclr;
mattress=mtres;
cout<<model_name<<item_number<<frame<<frameColour<<mattress<<endl;
}
Main section that is causing the problem:
Item* item= new Bed(number, name, material, colour, mattress);
cout<<"working, new bed"<<endl;
v.push_back(item);
cout<<"working pushback"<<endl;
cout<<" this is whats been stored:"<<endl;
cout<<v[count]->getName<<endl;
cout<<v[count]->getNumber<<endl;
count++;
when the programme executes, the cout within the constructor shows the correct output, but when I call getname and getnumber from the main function the programme prints '1' for both , no matter what's stored in there.
I thought that derived classes could use base class methods, what have I missed?
Any help would be great
thanks
Hx
Well, you example is not related to polymorphism. The reason here is that you are not using any virtual functions. Here is the code that you can use.
class Item
{
protected:
std::string model_name;
int item_number;
public:
Item();
Item(std::string& name, int number) : model_name(name), item_number(number) {};
std::string getName(){return model_name;}
int getNumber(){return item_number;}
};
class Bed : public Item
{
private:
std::string frame;
std::string frameColour;
std::string mattress;
public:
Bed();
Bed(int number, std::string& name, std::string& frm, std::string& fclr, std::string& mtres) : Item(name, number),
frame(frm),
frameColour(fclr),
mattress(mtres) {};
};
int main()
{
int count = 0;
std::vector<Item*> v;
Item* item = new Bed(2, std::string("MyBed"), std::string("wood"), std::string("red"), std::string("soft"));
std::cout << "working, new bed" << std::endl;
v.push_back(item);
std::cout << "working pushback" << std::endl;
std::cout << " this is whats been stored:" << std::endl;
std::cout << v[count]->getName() << std::endl;
std::cout << v[count]->getNumber() << std::endl;
++count;
getchar();
}
This looks incorrect (and I am unsure how this even compiled):
cout<<v[count]->getName<<endl;
cout<<v[count]->getNumber<<endl;
as getName and getNumber are methods. Change to:
cout<<v[count]->getName()<<endl;
cout<<v[count]->getNumber()<<endl;
Additionally, the initialisation of count is not posted: ensure it is zero.
count appears to be the size of your vector. After you push back the last element, you're not incrementing count, so you're printing an older element.
Why don't you try:
cout<<v[v.size()-1]->getName<<endl;
cout<<v[v.size()-1]->getNumber<<endl;
Also, you should start using initialization lists in your constructors:
Bed::Bed(int number, string name, string frm, string fclr, string mtres) :
Item(name,number),
frame(frm),
frameColour(fclr),
mattress(mtres)
{
}
You have not called the base class's constructor from the derived class... Where as it should be the first line... update the code, I am sure it will start working..
EDIT
If it doesn't than you should probably also check the way you are handling count variable... as pointed out by others..