009 Adding public methods that access private class attributes

Instructions

Given the following Dog class in Dog.h add the following public getter and setter methods that get and set each class attribute as follows:

get_name returns the name attribute of the dog 

set_name sets the name attribute of the dog to the string that is passed into the method.

get_age returns the age attribute of the dog

set_age sets the age attribute of the dog to the int passed into the method.

Please add your code to the Dog.h file. The exercise.cpp file should NOT be modified.

You can find my solution by clicking on the solution.txt file on the left pane. But please make sure you give it a go yourself first, and only check the solution if you really get stuck.

Test(s)

Test 1

#include "gtest/gtest.h"
#include "helpers/iohelper.h"

#include "Dog.h"

namespace {

class Evaluate : public ::testing::Test {};


TEST_F(Evaluate, ExampleTest) {
    // TODO
    Dog t;
    t.set_name("Spot");
    EXPECT_EQ("Spot", t.get_name()) << "The dog's name should be 'Spot'";
    t.set_age(5);
    EXPECT_EQ(5, t.get_age()) << "The dog's age should be 5";
}

}  // namespace

Solution(s)

Solution 1

//---THIS IS AN EMPTY FILE

Solution 2

#ifndef __DOG_H__
#define __DOG_H__
#include 
using namespace std;

class Dog {
private:
    string name;
    int age;
public:

//---- WRITE YOUR CLASS FUNCTIONS BELOW THIS LINE----
    string get_name() {
        return name; 
    }
    
    void set_name(string n) {
        name = n;
    }
    
    int get_age() {
        return age; 
    }
    
    void set_age(int a) { 
        age = a;
    }
};


//---- WRITE YOUR CLASS FUNCTIONS ABOVE THIS LINE----
#endif 

Solution 3

//