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

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.).

Related

Pass data from object in class A to class B

New to classes and objects in c++ and trying to learn a few basics
I have the class TStudent in which the Name, Surname and Age of student are stored, also I have the constructor which is accessed in main and inserts in the data.
What I want to do is: having the class TRegistru, I have to add my objects data in it, in a way that I can store it there, then I could save the data in data.bin and free the memory from the data, then I want to put the data back in the class and print it out.
The question is: In what way & what is the best way to add my objects in the second class, so that I could eventually work with them in the way I've described in the comments, so that I won't have to change nothing in main
Here's my code so far:
#include <iostream>
using namespace std;
class TStudent
{
public:
string Name, Surname;
int Age;
TStudent(string name, string surname, int age)
{
Name = name;
Surname = surname;
Age = age;
cout <<"\n";
}
};
class TRegistru : public TStudent
{
public:
Tregistru()
};
int main()
{
TStudent student1("Simion", "Neculae", 21);
TStudent student2("Elena", "Oprea", 21);
TRegistru registru(student1);//initialising the object
registru.add(student2);//adding another one to `registru`
registru.saving("data.bin")//saving the data in a file
registru.deletion();//freeing the TRegistru memory
registru.insertion("data.bin");//inserting the data back it
registru.introduction();//printing it
return 0;
}
Hence the question is about passing data from A to B, I will not comment on the file handling portion.
This can be done in multiple ways, but here is one of the simplest and most generic. By calling TRegistru::toString() you serialize every TStudent added to TRegistru into a single string which then can be easily written to a file.
Demo
class TStudent
{
public:
std::string Name, Surname;
int Age;
std::string toString() const
{
return Name + ";" + Surname + ";" + to_string(Age);
}
};
class TRegistru
{
public:
void add(const TStudent& student)
{
students.push_back(student);
}
void deletion()
{
students.clear();
}
std::string toString() const
{
std::string ret{};
for(const auto& student : students)
{
ret += student.toString() + "\n";
}
return ret;
}
std::vector<TStudent> students;
};

C++ Null output when a function is called

Below is a snippet of code from my main program
My H file
class Person{
public:
std::string name;
int rangeStance;
int initialStance;
Person(std::string name, int rangeStance, int initialStance){
name = name;
rangeStance = rangeStance;
initialStance = initialStance;
setName(getName());
setRangestance(getRangeStance());
setinitalStance(getRangeStance());
}
Person();
void setName(std::string name);
void setRangestance(int range);
void setinitalStance(int stance);
std::string getName();
int getRangeStance();
int getinitalStance();
double impact(int rangeStance, int initalStance);
};
class Leader: public Person {
public:
int popularity;
int totalcountryVotes;
Leader(std::string name, int rangeStance, int initialStance,int popularity, int totalcountryVotes)
:Person(name, rangeStance, initialStance), popularity(popularity), totalcountryVotes(totalcountryVotes){
popularity = popularity;
totalcountryVotes = totalcountryVotes;
setPopularity(getPopularity());
setTotalcountryVotes(getTotalcountryVotes());
}
Leader();
void setPopularity(int popularity);
void setTotalcountryVotes(int totalcountryVotes);
int getPopularity();
int getTotalcountryVotes();
};
The corresponding functions in the main cpp file.
Person::Person() {
}
void Person::setName(string Name)
{
name = Name;
}
string Person::getName() {
return name;
}
void Person::setRangestance(int Range)
{
rangeStance = Range;
}
int Person::getRangeStance() {
return rangeStance;
}
void Person::setinitalStance(int stance)
{
initialStance = stance;
}
int Person::getinitalStance() {
return initialStance;
}
Leader::Leader() {
}
void Leader::setPopularity(int popularity) {
popularity = popularity;
}
void Leader::setTotalcountryVotes(int totalcountryVotes) {
totalcountryVotes = totalcountryVotes;
}
int Leader::getPopularity() {
return popularity;
}
int Leader::getTotalcountryVotes() {
return totalcountryVotes;
}
Within main the needed funtions are called appropriately
int main(int argc, char* argv[]) {
Leader labourLeader("George Lopez",100,50,50, 75);//sets record for the labour party leader
cout << "--Party Leader--" << endl;
cout << labourLeader.getName() << endl;
return 0;
}
However when this snippet of code is compiled, no outcome is returned where it should be printing out "George Lopez". Im fairly "noob" with c++, am i using my contructor right or should I be delcaring it within my h file? Thankyou.
A couple of things wrong in this code
Person(std::string name, int rangeStance, int initialStance){
name = name;
rangeStance = rangeStance;
initialStance = initialStance;
setName(getName());
setRangestance(getRangeStance());
setinitalStance(getRangeStance());
}
Firstly it's not necessary to call setters and to do assignments, so lets drop those, leaving
Person(std::string name, int rangeStance, int initialStance){
name = name;
rangeStance = rangeStance;
initialStance = initialStance;
}
Now think about what name = name does. Does that look curious to you at all? It takes the parameter name and assigns it to the parameter name! The member variable also called name is completely unchanged. This situation where one name hides another similar name is called shadowing.
Person(std::string name, int rangeStance, int initialStance) {
name = name;
What's happening there is that it's just overwriting the parameter with itself, rather than copying it to the member variable. That's because the name lookup rules for unqualified names at that point prefer the parameter to the member variable. That means the member variable is being left at its constructed state, an empty string.
There are a few ways to fix this. The first is to simply name them differently so that there's no ambiguity, such as the common method of prefixing member variables with m_. That way, the statement becomes the more explicit:
m_name = name;
Another alternative is to be explicit about the one you're assigning to so that it's no longer unqualified:
this->name = name;
A third is to use initialiser lists where the rules are slightly different in that it uses the member variable outside the parentheses and does normal unqualified lookup within the parentheses:
Person(std::string name, int rangeStance, int initialStance)
: name(name)
, rangeStance(rangeStance)
, initialStance(initialStance)
// ^ ^
// | |
// | +- normal lookup, passed-in parameter.
// +--------------- member variable.
{
};
And there's no need to have all those other statements in the constructor, such as setName(getName()), since you've already set the name.

Inheritance and small number of parameters

Uncle Bob in his Clean Code suggests that no more than 3 arguments should a function get:
Functions that take three arguments are significantly harder to
understand than dyads. The issues of ordering, pausing, and ignoring
are more than doubled. I suggest you think very carefully before
creating a triad.
But what about CTOR arguments in class inheritance hierarchy? What if each class in hierarchy adds a new field and you should initialize them in CTOR. See an example below:
class Person
{
private:
std::string m_name;
int m_age;
public:
Person(const std::string& name, const int age);
std::string getName() const { return m_name; }
int getAge() const { return m_age; }
~Person();
};
class Student : public Person
{
private:
std::string m_university;
int m_grade;
public:
Student(const std::string& name, const int age, const std::string& university, const int grade);
std::string getUniversity() const { return m_university; }
int getGrade() const { return m_grade; }
~Student();
};
See how Student gets 4 arguments, while Person gets only 2 and Student adds two more. So how we should handle this?
There are several ways.
Combine multiple parameters into a struct
struct PersonInfo {
std::string name;
int age;
};
struct StudentInfo {
PersonInfo person_info;
std::string university;
int grade;
};
Person::Person(const PersonInfo &info) :m_name(info.name), m_age(info.age) {}
Student::Student(const StudentInfo &info) : Person(info.person_info), m_university(info.university), m_grade(info.grade) {}
Default initialize data members and set them with setter utilities
Person::Person() : m_age(0) {}
void Person::set_age(int age) { m_age = age; }
Student() : m_grade(0) {} // Person is default constructed.
void Student::set_grade(int grade) { m_grade = grade; }
i'd say this was just a suggestion. it's fully up to you - how many arguments should your functions get.
but if you prefer to follow the rule, make some sort of parameters holder, like:
class Student
{
public:
struct StudentParameters
{
...
};
Student(name, age, const StudentParameters &sp);
...
};
You're confusing two distinct meanings of the word function.
The first meaning is more related to the original mathematical meaning of the word. In this case, function is a named relation between one or more inputs and exactly one output. The "Clean Code" rules refers to this meaning, and tells you that more should be limited to 3 inputs.
The alternative meaning in C++ refers to a block of code, which may or may have inputs, which may or may have an output, which may or may have a name.
And yes, even in the latter sense, constructors are unusual functions. They never have a return type, not even void, and they don't have names. So you can rationalize that they're also special when it comes to their number of input arguments.

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

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).

How can I create multiple items with one class in C++?

I have the class Furniture with:
Furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
~Furniture();
void setname(string name);
void setprice(double price);
double getprice();
string getname();
virtual void printSpecs();
private:
string name;
double price;
protected:
static int NumberOfItems;
int Id;
};
furniture.cpp:
#include "furniture.h"
Furniture::Furniture() {
}
Furniture::~Furniture() {
}
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
double Furniture::getprice() {
return this->price;
}
void Furniture::printSpecs() {
cout<<"Price: "<<this->price<<endl;
cout<<"Name: "<<this->name<<endl;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
model->printSpecs();
delete model;
}
Everything works fine but I want to add multiple furniture items with the same class and just update the NumberOfItems. Is there any way to do that?
Also, is my code ok? I mean, how can I improve it? I'm quite new to OOP and I'd like to learn some good practices.
Thanks.
The idea is conceptually broken. You cannot do that; you really need different objects.
Alternatively, if you really want to have multiple identical items, you can create one item and create multiple pointers to it, and maintain a separate count for the number of active items. A shared_ptr does that for instance.
That said, your code shouldn’t use pointers at all, this is a common anti-pattern in C++ code. Furthermore, your code probably shouldn’t have setters, provide a proper constructor instead:
int main() {
Furniture model("FinalDestiny", 149.99);
model.printSpecs();
}
Much shorter, simpler, and no possiblity of leaking memory.
To keep track of the number of items, you can update the number of items in the constructor:
Furniture::Furniture() {
Id = NumberOfItems++;
}
and decrement in the destructor if you want:
Furniture::~Furniture() {
NumberOfItems--;
}
To access the item by Id, you need to have an extra manager class or use a map:
std::map<int,Furniture*> items;
which you can pass as parameter to the constructor and update it there:
Furniture::Furniture(std::map& items) {
Id = NumberOfItems++;
items[Id] = this;
}
And, outside, you can simply retrieve items with:
Furniture* f = items[3];
I would write in this way
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture(string name = "", double price = 0)
: name(name), price(price), Id(NumberOfItems++)
{}
Furniture(const Furniture &f)
: name(f.getname()), price(f.getprice()), Id(NumberOfItems++)
{}
void setname(string name) { this->name = name; }
void setprice(double price) { this->price = price; }
double getprice() const { return price; }
string getname() const { return name; }
virtual void printSpecs() {}
private:
string name;
double price;
protected:
static int NumberOfItems;
int Id;
};
int Furniture::NumberOfItems;
int main_furniture(int, char **)
{
Furniture a("product 1", 100);
Furniture x(a), y(a), z(a);
}
I've inlined just to simplify. What's interesting to you should be the copy constructor implementation, and (OT) you forget the const on getter...
Just increment NumberOfItems in the constructor, and decrement it in the destructor.
Store the furniture instances in an array or better in a vector. You can access them with an index or iterator. The NumberOfItems field doesn't belong in the furniture class, an instance of furniture shouldn't know about how many furniture items there are in the system. Use the size () method from vector to get the furniture item count.