For example, I have this class:
struct bankingFunctionality
{
private:
class bankingData
{
public:
int phoneNum, citizenNum, age, ID;
string name, address;
bankingData* next = nullptr;
};
public:
void input(bankingData*);
void firstUser();
void update(bankingData*);
};
Is it ideal to put the data class in private to "protect" data? Since the only way, I can think of how to access it for input and output is to have functions like void input(...) in public. However, every time I want to call input I would need to create a new object and waste memory.
If you're using this as a way to pass in arguments it's really wasteful and you should consider the traditional getX() and setX() approach for accessors and mutators. You can make these easy to use in a chain style if you return *this at the end of each.
As in:
class bankingData {
int phoneNum, citizenNum, age, ID;
string name, address;
bankingData* next = nullptr;
int getPhoneNum() const { return phoneNum; };
bankingData& setPhoneNum(int n) { phoneNum = n; return *this; };
const std::string& getName() const { return name; };
bankingData& setName(const std::string& n) { name = n; return *this; };
};
Where you can then do:
bankingData bd;
bd.setPhoneNum(111).setName(...);
Related
I have made a University class that consists of vector where Degree is another class consisting of vector. All the vectors are private and I would like to use setters and getters to modify them. While testing its functionality in main() I have noticed weird behaviour:
University ua;
ua.setDegree(Degree("Computer Science"));
ua.getDegree(0).setStudent(Student("1101","Ria",true));
ua.getDegree(0).setStudent(Student("1102","Ava",false));
ua.setDegree(Degree("Biotechnology"));
ua.getDegree(1).setStudent(Student("1104","Vry",false));
ua.showDegrees();
While adding new Degrees to the ua object I can see the result while displaying them, however when I add new Student to these Degrees I don't see any change. Both Degrees remain empty. I feel like I edit their local copy and therefore I don't see the added Students. However, I don't know how could I fix it... This it the code of the setters:
class University {
private:
vector<Degree> all;
int n;
public:
Degree getDegree(int i) {return all.at(i);}
void setDegree(Degree degree) {all.push_back(degree); n++;}
int getN() {return n;}
void showDegrees() { for(int i=0; i<n; i++) {cout<<endl<<getDegree(i).getName()<<endl;} }
University() { this->n = 0; } };
class Degree {
private:
string name;
vector<Student> all;
int n;
public:
string getName() {return name;}
void setName(string name) {this->name = name;}
Student getStudent(int i) {return all.at(i);}
void setStudent(Student student) {all.push_back(student); n++;}
vector<Student> getAll() {return all;}
int getN() {return n;}
Degree(string);
Degree() {} };
Degree::Degree(string name) {
this->name = name;
n = 0;
}
Your getters return copies of the objects contained within your class. If you want to be able to directly modify the class objects, you need to return by reference:
Degree &getDegree(int i);
Student &getStudent(int i);
How can I copy all the objects to another set of objects. I created a temporary object so I can add the current "robot" to the "robotSquad". I am trying to dynamically add it
I am new to the concept of operator overloading, I might have the wrong assumption on what I'm doing. Im using the += operator, making a temp if there is no space in "robotSquad" adding it to temp then copying it to the original robotSquad named r_robot.
I could possibly be doing all this wrong, need help Thanks!
RobotSquad& RobotSquad::operator+=(const Robot& p_robot) {
if (count <= sizeof(r_robots)) {
//how do i copy?
RobotSquad temp[count+1];
temp[count]=p_robot;
for(int i=0 ;i<=sizeof(r_robots);i++){
temp[i]=r_robots[i];
}
} else {
this ->r_robots[count]=p_robot;
}
count++;
return *this;
}
class RobotSquad {
char* name ;
Robot *r_robots;
int count;
public:
//constructor
RobotSquad();
RobotSquad(Robot* , int, char* p_name= "No name");
~RobotSquad();
//getters
char* getName();
Robot* getRoster();
int getCount();
//setters
char setName();
Robot setRobot();
int setCount();
//other
RobotSquad& operator+=(const Robot&);
private:
void setEmpty();
};
With std, it could be:
class RobotSquad {
std::vector<Robot> robots;
std::string name;
public:
//constructor
RobotSquad() = default;
explicit RobotSquad(const std::vector<Robot>& robots,
const std::string& name= "No name")
: robots{robots}, name{name}
{}
~RobotSquad() = default;
RobotSquad& operator+=(const Robot& robot)
{
robots.push_back(robot);
return *this;
}
// ...
};
I am a newbie in c++ and today I was trying to make access a public class member outside of main(), more exactly in another function.
I tried to create pointer to that class, but I fail at accessing its members. I am going to show an example with a few lines of code, Any help would be appreciated.
Class City
{
private:
string name;
public:
string getName()
{
return name;
}
};
bool isCity(string input)
{
if(input== ???) { return true; }
return false;
}
*The problem: how to access public member getName() from the class I create in main() at the question marks
int main()
{
string input;
City test;
cin >> input;
isCity(input);
cin.get();
}
The pointer to Class is not working, the reference pass isn't working either.
Easiest way is to have isCity part of the class and call it as a method of the object test.isCity(input);
The class should be as follow:
Class City
{
private:
std::string name;
public:
std::string getName() const
{
return name;
}
bool isCity(const std::string& input) const
{
return input.compare(name) == 0
}
};
Else you could have a free function (outside of the class), but the signature should provide the class city object as follow:
bool isCity(const City& c, const std::string& input)
{
return input.compare(c.GetName()) == 0;
}
Which then means you have to call the function as follow:
isCity(test, input);
I have a little problem .. I want to do car registration and i want to have owner(pointer to a Person object) but when i want to take the owner with get function i can't do that ... and this is mystery for me ... Maybe i have a little mistake somewhere and i can't find it .. Please help :)
class Person
{
friend class Cars;
public:
Person(){}
Person(uint8_t age, string name) :m_age(age), m_name(name)
{
m_idGener = s_idGenerator++;
}
int getId()
{
return m_idGener;
}
uint8_t getAge()
{
return m_age;
}
string getName()
{
return m_name;
}
private:
string m_name;
uint8_t m_age;
static int s_idGenerator;
int m_idGener;
};
class Cars:public Person
{
public:
Person *m_owner; // this is my pointer which i want to point to cars Object
it will stay public for the test after the test i will move it in the private section
Cars()
{
}
// getters and setters
Cars setOwner(Cars &object, Person &owner) // with this function i set the owner
{
object.m_owner = &owner;
}
Cars getOwner(Cars &object) /// here is my problem i can't take the owner
{
return *(object.m_owner); // but i can't take the owner
}
uint16_t getHorsePower()
{
return horsePower;
}
string getRegNumber()
{
return registrationNumber;
}
private:
string m_manufacturerName;
string modelName;
uint16_t horsePower;
string registrationNumber;
};
int Person::s_idGenerator = 0;
You have made more mistakes. At least the followings should be corrected:
Cars setOwner(Cars &object, .....
This method doest not return a value, so it should be void. The car object should be the object for which the method will be called, not a parameter.
Cars getOwner(Cars &object) /// here is my problem i can't take the owner
{
return *(object.m_owner); // but i can't take the owner
}
In this method I would change the return type to Person *, then return simply m_owner. Parameter is not needed.
A modified working example:
#include <string>
#include <iostream>
using namespace std;
class Person
{
friend class Cars;
public:
Person(){}
Person(uint8_t age, string name) :m_age(age), m_name(name)
{
m_idGener = s_idGenerator++;
}
int getId()
{
return m_idGener;
}
uint8_t getAge()
{
return m_age;
}
string getName()
{
return m_name;
}
private:
string m_name;
uint8_t m_age;
static int s_idGenerator;
int m_idGener;
};
class Cars:public Person
{
public:
Person *m_owner; // this is my pointer which i want to point to cars Object
// it will stay public for the test after the test i will move it in the private section
Cars()
{
}
// getters and setters
void setOwner(Person &owner) // with this function i set the owner
{
m_owner = &owner;
}
Person *getOwner() /// here is my problem i can't take the owner
{
return (m_owner); // but i can't take the owner
}
uint16_t getHorsePower()
{
return horsePower;
}
string getRegNumber()
{
return registrationNumber;
}
private:
string m_manufacturerName;
string modelName;
uint16_t horsePower;
string registrationNumber;
};
int Person::s_idGenerator = 0;
int main() {
Person p(5,"Bill");
Cars c;
c.setOwner(p);
cout << c.getOwner()->getName();
}
Here:
Cars setOwner(Cars &object, Person &owner) // with this function i set the owner
{
object.m_owner = &owner;
}
You declared out value type as "Cars" but you don't return anything.
And here:
Cars getOwner(Cars &object) /// here is my problem i can't take the owner
{
return *(object.m_owner); // but i can't take the owner
}
You want to return "Person" object but again you declared out value type as "Cars".
So you can change this functions like that:
First:
void setOwner(Cars &object, Person &owner);
Second:
Person getOwner(Cars &object);
class cat
{public:
void dog(int ID, char *value) // int ID I'd like to be the index array it was called from?
{
debug(ID, value);
}
}
cat cats[18];
cats[1].dog("value second arg, first arg auto filled from index array");
I want something similar to this.
include <vector>
class CatArray;
class Cat {
// This line means that the CatArray class can
// access the private members of this class.
friend class CatArray;
private:
static int ID;
public:
void dog(const char* value) {
// Use ID here any way you want.
}
};
int Cat::ID = 0;
class CatArray {
private:
std::vector<Cat> cats;
public:
explicit CatArray(unsigned int size) : cats(size) {}
Cat& operator [](unsigned int index) {
Cat::ID = index;
return cats[index];
}
};
But a little different. There are 18 Clients in a game and i need to basically do this. for eg, "Client 4 Chooses an option and the option gets called through the array index and than that way client 4 will call the function with the function holding the index 4"
Then cats[1] is not really a Cat object but a CatWithIndex object:
class Cat {
public:
void dog(size_t index,const char* value);
};
class CatWithIndex {
size_t index_;
const Cat &cat_;
public:
CatWithIndex(size_t index, const Cat &cat): index_(index), cat_(cat) {}
void dog(const char* value) {
cat_.dog(index_,value);
}
};
class CatArray {
private:
std::vector<Cat> cats;
public:
Cat& operator [](unsigned int index) {
Cat::ID = index;
return CatWithIndex(index,cats[index]);
}
};