c++ setter/getter does not work as expected - c++

for some reason my prof insists I have to use setter/getter everywhere.
Well... in this linked list I don't understand why this one doesn't work
#include <iostream>
#include <string>
using namespace std;
class SomeClass{
string some_string;
public:
void setSomeString(string s);
string getSomeString();
};
void SomeClass::setSomeString(string s){
some_string = s;
}
string SomeClass::getSomeString(){
return some_string;
}
class Node{
SomeClass some_class;
Node *next;
public:
void setSomeClass(SomeClass sc);
SomeClass getSomeClass();
//...
};
void Node::setSomeClass(SomeClass sc){
some_class = sc;
}
SomeClass Node::getSomeClass(){
return some_class;
}
class List{
public:
Node *head, *ptr;
//...
};
int main(){
List l;
l.head = new Node();
l.head->getSomeClass().setSomeString("a string");
cout << l.head->getSomeClass().getSomeString();
return 0;
}
I expect in the output "a string", instead is empty... What am I doing wrong?

Node::getSomeClass() method returns something by value. This means that the returned object is a copy which means that now you have two different objects and there is no side effect.
If you want to edit the original value you need to return it by reference:
SomeClass& getSomeClass() {...}
In this way you will get the reference of the original object and you can edit it.

Don't wrap something if wrapper adds nothing to wrapped like SomeClass adds nothing to string except useless set/get.
Even if Node::getSomeClass() returns by reference why it's private in Node? If private member returned by reference it can be abused outside of Node.
If you insist to make SomeClass private in Node then l.head->getSomeClass().setSomeString("a string") should be l.head->setSomeString("a string") where:
void Node::setSomeString(string input){
some_class.setSomeString(input); //while not exposing some_class at all
}

Related

Destructor for pointer and non pointer

Why this can't work. Is there any way to do this?
I don't want to create a separate function for pointers
#include <iostream>
using namespace std;
template<class T>
class temp
{
public:
T val;
temp(T value) : val(value) {}
~temp()
{
if(is_pointer<T>::value)
{
delete val;
}
}
};
int main()
{
string * n = new string("cat");
temp<string*>object(n);//ok
temp<string>object2("dog"); //compliation error: type 'class std::cxx11::basic_string' argument given to 'delete', expected pointer. --- (but there is if statement!!!!)
//i dont want delete in main
return 0;
}
To compile i use g++ 6.3.0
Could someone help? Maybe, I need to separate declaration from definition?
The issue that you have is that the branch of an if must always be syntactically valid, even if it is never taken.
You could do it with if constexpr, which is a "compile time if"
~temp()
{
if constexpr(is_pointer<T>::value)
{
delete val;
}
}
However this isn't safe.
How do you know that the pointer passed to temp<T*> was created by new and not new[], malloc, or by taking the address of an object that wasn't dynamically allocated?
Rather than assume that pointers should be deleted, you should avoid having to know which pointers to delete
#include <string>
#include <memory>
template<class T>
class temp
{
public:
T val;
temp(T value) : val(value) {}
// n.b. no need to define destructor
};
int main()
{
std::string str("cat");
temp<std::string*> object(&str);//ok
temp<std::string> object2("dog"); // also ok
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("mouse");
temp<std::string *> object3(str2.get()); // ok so long as str2 outlives object3
std::shared_ptr<std::string> str3 = std::make_shared<std::string>("rabbit");
temp<std::shared_ptr<std::string>> object4(str3); // also ok
return 0;
}

class modify via set/get methods

trying to modify object in the class via get/set methods. I can't understand how change value just only use get/set method.
expected output : "Output : 89".
actual output : "Output : 0"
#include<iostream>
using namespace std;
class TestClass{
public:
int getValue() const{
return _value;
}
void setValue(int value) {
_value = value;
}
private:
int _value;
};
class A{
public:
TestClass getTestClass() const{
return _testClass;
}
void setTestClass(TestClass testClass) {
_testClass = testClass;
}
private:
TestClass _testClass;
};
int main()
{
A a;
a.getTestClass().setValue(89);
cout<<"Output :"<<a.getTestClass().getValue();
}
replace
TestClass getTestClass() const{
return _testClass;
}
with
TestClass& getTestClass() {
return _testClass;
}
You want to return a reference otherwise you are just returning a copy of the variable. But Keep in mind that returning a (non-const) reference to the member variables of a class is not a good design approach.
Some things:
please don't use using namespace std; - read here why.
please don't name your variables _testClass - go with m_testClass instead. You can read hear about the reasoning.
You're returning a copy of _testClass. So when you modify it with setValue(89), nothing happens because you're only modifying a copy that is discarded at the end of the line. Instead, you should return a reference.
Change this here:
TestClass getTestClass() const{
To this:
TestClass &getTestClass() {
And you get the expected output.

Modify class private outside class without using public method

I am a C++ newbie and I need help with a strange issue (or at least its strange to me)
I have a class as such:
class Myclass {
private:
int A;
// some other stuff...
public:
// constructor and stuff...
void setA(int a);
int* getA_addr();
};
void Myclass::setA(int a){
A = a;
};
int* Myclass::getA_addr(){
return &A;
};
Now, I want to modify A in main() and I am not using any other methods in the class (I did it by using extra methods and now I want to see how I can do it without using those extras). I have a function as such:
void change(int *ptr, int tmp){
*ptr = tmp;
};
In a call to this function, I do the passing as such: change(obj.getA_addr(), other arguments...) where obj is an instance of Myclass.
When done in this way, I receive no compilation errors but I also can't seem to modify A (of obj). As a debug effort, I tried to print the address of A (of obj) by directly calling getA_addr(). I saw that with every call, the function returns a different address. So I am assuming that I am not passing the intended address into the function.
I have no idea why this is happening and would like to know. Also, the way I'm trying to do this is most likely not at all accurate so please, if you can provide a solution, it would be appreciated. Thanks.
EDIT: Here's the most minimal code I could come up with that reproduces the error
#include <iostream>
#define MAX_SIZE 10
using namespace std;
class Student {
private:
int mt1;
public:
Student();
void setMt1(int in_mt1);
int* getMt1();
};
Student::Student() {};
void Student::setMt1(int in_mt1) { mt1 = in_mt1; };
int* Student::getMt1(){ return &mt1; };
class Course {
private:
Student entries[MAX_SIZE];
int num;
public:
Course();
void addStudent(Student in_student);
Student getStudent(int index);
};
Course::Course(){ num = 0; };
void Course::addStudent(Student in_student){
entries[num] = in_student;
num++;
};
Student Course::getStudent(int index){ return entries[index]; };
int main() {
void updateStudentScore(int *uscore, int newscore);
Course mycourse;
Student tmp_student;
tmp_student.setMt1(60);
mycourse.addStudent(tmp_student);
cout<<mycourse.getStudent(0).getMt1()<<"\t"<<*mycourse.getStudent(0).getMt1()<<endl;
updateStudentScore(mycourse.getStudent(0).getMt1(), 90);
cout<<mycourse.getStudent(0).getMt1()<<"\t"<<*mycourse.getStudent(0).getMt1()<<endl;
return 0;
}
void updateStudentScore(int *uscore, int newscore){
*uscore = newscore;
};
I am fairly certain that my understanding of pointers and passing-by-whatevers is lacking and the way I defined functions here is creating the bug. I am sorry to inconvenience you guys. I would appreciate it if you could take a look.
Looking at your student/course code, i notice that Course::getStudent returns a Student rather than a Student &. Basically, that means each time you call getStudent(x), you get a temporary copy of student x, whose getMt1 function will give you a pointer to a temporary field, and any changes you make won't even survive past that statement.
If you have getStudent return a reference or pointer instead, your changes should persist to the Student contained in the array. (They still won't affect tmp_student, though, because addStudent copied it to add it to the array. If you want that reliably, then you need to redo quite a bit of stuff.)

Initialise values of a struct for use in another file

So I have my class Test.H which has a struct in it.
Class Test{
private:
struct Data
{
char *first;
int number;
int count;
};
Data *myStruct;
I am trying to use the myStruct in my User.C initialiser.
//User.C
#include "Test.H"
Test::Test(const char *alp){
myStruct.number = 0;
}
And I get an segmentation fault and error from valgrind.
I figured initially that it was due to Data* myStruct being in private, but after writing a function like this below:
Data getStruct(){
return myStruct;
}
It will still give me errors when I use it in User.C
You need to construct the struct before you can do anything with it. Calling the default constructor will initialises it's fields to 0.
You had class capitalised, that was wrong. I also don't see a reason why it has to be a pointer, so I removed that. Lastly, I added a destructor in Data so that first will be deleted. I assume it is a cstring, so I used delete[]. If it is something else, delete it in whatever manner is appropriate.
If you must have a pointer, modify the call to the constructor to use new, delete the struct in the destructor ~Test(), and reference members of myStruct with the -> operator.
class Test {
public:
struct Data {
const char* first;
int number;
int count;
~Data() {
delete[] first;
}
};
Test(const char *alp) {
// Default constructor initialises struct's fields to 0.
myStruct = Data();
myStruct.count = 7;
}
private:
Data myStruct;
};

C++ Container and Entity Classes

I'm new to the site (and to programming) so I hope I post this question appropriately and under all the proper guidelines of the site. Ok, here it goes:
So I pretty new to C++ and am trying to create classes for a program. I have to construct "container and entity classes", but where I'm struggling is trying to nail down the proper syntax for my getter and setter functions in the container class. So here's the code I have so far:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
const int MAX_STUDENTS=100;
const int MAX_COURSES=25;
const int NAME_SIZE=30;
const int COURSE_COLUMNS=4;
const int GRADE_ROWS=10;
//Entity Classes
class Course
{
//Two private member variables
private:
string courseText;
int courseID;
public:
//Constructor
Course(void)
{
//Just providing initial value to the two object variables
courseText;
courseID=-1;
}
//Setters and Getters for each variable
string getCourseText(){
return courseText;}
void setCourseText(string userEnteredText){
courseText = userEnteredText;}
int getCourseID(){
return courseID;}
void setCourseID(int userEnteredID){
courseID = userEnteredID;}
};
class Student
{
//Private member variables
private:
string studentText;
int studentID;
int** coursesAndGrades;
int enrolledCoursesCount;
int timesReallocatedColumns;
int timesReallocatedRows;
public:
//Constructor
Student(void)
{
//Just providing initial value to the object variables
studentText;
studentID=-1;
coursesAndGrades = new int*[GRADE_ROWS+1];
for(int i=0;i<(GRADE_ROWS+1);i++)
{
coursesAndGrades[i] = new int[COURSE_COLUMNS];
}
enrolledCoursesCount=0;
timesReallocatedColumns=0;
timesReallocatedRows=0;
}
//Setters and Getters for each variable
string getStudentText(){
return studentText;}
void setStudentText(string userEnteredText){
studentText = userEnteredText;}
int getStudentID(){
return studentID;}
void setCourseID(int userEnteredID){
studentID = userEnteredID;}
int getCoursesAndGrades(int gradeRow, int courseColumn){
return coursesAndGrades[gradeRow][courseColumn];}
void setCoursesAndGrades(int gradeRow, int courseColumn, int entry){
coursesAndGrades[gradeRow][courseColumn]=entry;}
int getEnrolledCoursesCount(){
return enrolledCoursesCount;}
void setEnrolledCoursesCount(int enrolledCount){
enrolledCoursesCount = enrolledCount;}
int getTimesReallocatedColumns(){
return timesReallocatedColumns;}
void setTimesReallocatedColumns(int reallocColumnCount){
timesReallocatedColumns = reallocColumnCount;}
int getTimesReallocatedRows(){
return timesReallocatedRows;}
void setTimesReallocatedRows(int reallocRowCount){
timesReallocatedRows = reallocRowCount;}
};
Now, I've got a container class called GradeBook which contains dynamically allocated arrays of these two entity class objects.
class GradeBook
{
private:
Course* courses;
Student* students;
public:
//Constructor
GradeBook(void)
{
courses = new Course [MAX_COURSES];
students = new Student [MAX_STUDENTS];
}
}
I'm trying to figure out the proper way to translate the setter and getter functions from my entity classes to the container class so I can change individual elements of each class object in the dynamically allocated array. These changes will happen in more public member functions in the container class, but I'm completely stumped. I hope this question makes sense, and I'm not looking for anyone to write all of the setters and getters for me, I just need someone to point me in the proper direction for the syntax. Thanks everyone who made it through this!
If you will have something like this:
class GradeBook
{
public:
...
Student& student(int idx) { /*some boundary check here*/
return students[idx]; }
}
then you can use that method as:
GradeBook theBook;
...
auto idOfFirstStudent = theBook.student(0).getStudentID();
You just need to decide what that student() method shall return: it can return reference (as above) or pointer to student (instance). In later case you can return nullptr in case of out-of-bound errors. In first case the only reasonable option is to throw an error.
So there's no magic needed here, but you do need to decide how you want to do it. One way would be to just write something like:
void GradeBook::setCourseText(int i, const string &txt) {
courses[i].setCourseText(txt);
}
BTW, I would highly recommend using std::vector and at() rather than new.