C++ Null output when a function is called - c++

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.

Related

Getter Setter for class

I was working on homework that my instructor wanted me to write a class named Species with setter and getter functions. I wrote that code but I can't set or get any value when I run it. Can you help me?
class Species
{
private:
string name;
string country;
int population;
int growthrate;
public:
int year;
void setName(string NameS){
NameS=name;
}
void setCountry(string CountryS){
CountryS=country;
}
void setPopulation(int pop){
pop=population;
}
void setGrowthRate(int GrowRth){
GrowRth=growthrate;
}
void setYear(int syear){
syear=year;
}
string getName() {
return name;
}
string getCountry() {
return country;
}
int getPopulation() {
return population;
}
int getGrowthrate() {
return growthrate;
}
double e=2.71828182;
double calculatePopulation() {
int b=growthrate*year;
int a=pow(e,b);
return population*a;
}
};
First of all. Your class has fields like:
string name;
string country;
int population;
int growthrate;
And your methods are like:
void setName(string NameS){
NameS=name;
}
So you want to set NameS value to the name which makes no sense.
You should assign the field like name to be equal to nameS not the opposite.
Generally, a setter should look like this.
void setVariable(const VariableType& var){
this->var=var;
}
What you did was var=this->var.
Btw, you should make your getter-s const
You should use "this" keyword to set the value to object of the class.
this: to refer current class instance variable. The this keyword can be used to refer current class instance variable.
for example:
void setName(string name){
this.name=name;
}
void setGrowthRate(int growthrate){
this.growthrate=growthrate;
"this" is very helpful in please learn more about it.

C26495 Variable 'Employee::age' is uninitialized. Always initialize a member variable (type.6)

this is my code:-
#include<iostream>
using std::string;
//bydefault private cant access attributes outside class
class Employee{
public:
string name;
string company;
int age=0;
void Introduce() {
std::cout << "my age is- " << age << std::endl;
}
Employee(string company, string name, int age) {
name = name;
company = company;
age = age;
}
};
int main() {
Employee emp1 = Employee("bww","susmit",24);
emp1.Introduce();
//Employee emp2;
//same example
}
Output is my age is 0
I want it to be what I entered emp1 args to be
please help.
This constructor
Employee(string company, string name, int age) {
name = name;
company = company;
age = age;
}
is incorrect. Within the body of the constructor you are assigning parameters to themselves. It is because the parameters hide data members with the same name within the body of the constructor.
At least you should write
Employee(string company, string name, int age) {
this->name = name;
this->company = company;
this->age = age;
}
But it would be even better to use a mem-initializer list like
Employee(const std::string &company, const std::string &name, int age)
: name( name ), company( company ), age( age )
{
}
And in main you may write
Employee emp1("bww","susmit",24);
instead of
Employee emp1 = Employee("bww","susmit",24);
As for the function Introduce then it is better to declare and define it the following way
std::ostream & Introduce( std::ostream &os = std::cout ) const
{
os << "my age is- " << age << std::endl;
return os;
}
Also there is no great sense to declare these data members
string name;
string company;
int age=0;
as having a public access. You could declare them as having private access.
Also as your class does not have the default constructor then this member initialization within the class definition
int age=0;
is redundant.
Your constructor just assigns the arguments given to the constructor to themselves:
Employee(string company, string name, int age) {
// ^
// |
name = name; // -------------+ both left and right name refer to the argument
company = company;
age = age;
}
You could solve it by using a different name for the constructor arguments (or by being explicit using this->member = ...) or by using the member initializer-list:
Employee(string company, string name, int age) : // colon starts the member init-list
name(name), // The first name refers to the member varible (this->name)
company(company), // and the second refers to the constructor argument
age(age) //
{
// constructor body can now be empty
}

Why do I get this error "expression must be an lvalue or a function designator" for some of my data members?

I am trying to create a function that prints whatever I feed it from the class I created. I can only get some of my class object data members to work with my print function.
I have tried using pointers and references in some source codes I found online but they didn't work. After working on this problem for about a week now I feel like I have tried everything.
void print(string* object);
enum Degree { SECURITY = 1, NETWORKING = 2, SOFTWARE = 3};
class Student {
public:
Student(string ID, string first, string last, string Email, int Age, int days1, int days2, int days3, Degree StudentDegree) {
studentID = ID;
firstName = first;
lastName = last;
email = Email;
age = Age;
daysTillComplete[0] = { days1 };
daysTillComplete[1] = { days2 };
daysTillComplete[2] = { days3 };
studentDegree = StudentDegree;
return;
}
string getID() const;
void setID(string ID);
string getFirst() const;
void setFirst(string ID);
string getLast() const;
void setLast(string ID);
string getEmail() const;
void setEmail(string ID);
int getAge() const;
void setAge( int Age);
int getdays1();
int getdays2();
int getdays3();
void setDaysTilComplete(int days, int days1, int days2);
Degree getDegree() const;
void setDegree(Degree degreeType);
private:
string studentID = "No name";
string firstName = "No name";
string lastName = "No name";
string email = "No name";
int age = 7;
int daysTillComplete[3];
Degree studentDegree = NETWORKING;
};
void print(string* object) {
cout << *object << endl;
}
string Student::getID() const {
return studentID;
}
string Student::getFirst() const {
return firstName;
}
string Student::getLast() const {
return lastName;
}
string Student::getEmail() const {
return email;
}
int Student::getAge() const {
return age;
}
int Student::getdays1() {
return daysTillComplete[0];
}
int Student::getdays2() {
return daysTillComplete[1];
}
int Student::getdays3() {
return daysTillComplete[2];
}
Degree Student::getDegree() const {
return studentDegree;
}
void Student::setID(string ID) {
studentID = ID;
}
void Student::setFirst(string first) {
firstName = first;
}
void Student::setLast(string last) {
lastName = last;
}
void Student::setEmail(string Email) {
email = Email;
}
void Student::setAge(int studentAge) {
age = studentAge;
}
void Student::setDaysTilComplete(int days1, int days2, int days3) {
daysTillComplete[0] = days1;
daysTillComplete[1] = days2;
daysTillComplete[2] = days3;
}
void Student::setDegree(Degree degreeType) {
studentDegree = degreeType;
}
int main() {
Student vallery("A1", "Vallery", "Williams", "vallery.a.williams1234#gmail.com",21,52,28,32,NETWORKING);
print(&vallery.getID());
print(&vallery.getFirst());
print(&vallery.getLast());
print(&vallery.getEmail());
//print(&vallery.getAge()); <--- will not compile if this statement is in the program
//print(&vallery.getdays1()); <--- will not compile if this statement is in the program
////print(&vallery.getdays2()); <--- will not compile if this statement is in the program
////print(&vallery.getdays3()); <--- will not compile if this statement is in the program
//print(&vallery.getDegree()); <--- will not compile if this statement is in the program
cin.get();
return 0;
}
My expected results for example print(vallery.getFirst); would be print vallery. That works, but when I do print(vallery.getAge); it should be 21 but I can't even compile it because I get the "expression must be an l-value or function designator" error. Also using int instead of string for the data type of the print function doesn't work either. I must be using something incorrectly. Can someone point me in the right direction? After a bunch of research I haven't found any solutions.
There are several things are are probably going wrong with this code.
The most obvious is that your print function expects a string*. The calls that fail are those where you try and give the function something that is not a string*. For example, getAge() returns a int, where you are taking the address of the returned int. So you are trying to call print(int*) which then is failing. To solve this you can either using templating or have overloads. Here is a recommended template function:
template <typename T>
print (const T& _object)
{
cout << _object << endl;
}
A second problem, which is what is masking this first error comes from the fact you are taking a ptr instead of a reference (or a const reference as in my template function). If you want to print something, you want it to definitely be there and not a nullptr. So use references. In saying this, I think the ones that you say will work, definitely shouldn't work (even though they are string*'s) under any sane compiler. Perhaps the current error is hiding those errors.
The error “expression must be an lvalue or a function designator” comes from the fact that you are trying to take the address of an r-value. That is a value that is temporary and/or has not been bound to a name. Your getters return a copy of the value stored in your student object and this value has no address. You are trying to get the address of an addressless value, hence the error that it must be lvalue.
To solve this: Firstly don't use pointers, use a const reference. Secondly, have your getters return a const reference for objects. For example,
const string& Student::getID() const;
If your print function takes a const reference, it is able to handle a temporary variables for the cases where you return an int or other primitive type. Since your other getters return a reference to an addressable value, the print function can also take this reference without having to needlessly copy the values.

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

C++ Inheritance: Calling constructor w/ args

I'm trying to invoke my parent class's constructor that has arguments, in my child class's constructor with arguments, but I get a compiler error "expected primary expression before ...".
This is what I have:
class Ship {
private:
string shipName;
int yearBuilt;
public:
Ship();
Ship(string name, int year);
};
class CruiseShip: public Ship {
private:
int maxPeople;
public:
CruiseShip()
: Ship() {
maxPeople = 100;
}
CruiseShip(int m)
: Ship(string name, int year) {
maxPeople = m;
}
};
Ship::Ship() {
shipName = "Generic";
yearBuilt = 1900;
}
Ship::Ship(string name, int year) {
shipName = name;
yearBuilt = year;
}
And this is the specific piece of code I'm having trouble with:
CruiseShip(int m)
: Ship(string name, int year) {
maxPeople = m;
}
My goal is to be able to create an object, CruiseShip c1 with 3 arguments that set the name,year, & max people.
I've been reading online and it tells me that this should be ok, but I'm obviously doing something wrong. I'd appreciate any input, thanks!
You need to pass parameters to parent class constructor like this:
CruiseShip(int m, string name, int year): Ship(name, year), maxPeople(m) {}
Better, you should set maxPeople to m in the initializer list.