006 Creating and Accessing Objects

Instructions

We have already declared a C++ class named Dog  that is located in Dog.h (we'll learn about header files later in this section).

In the test_dog  function create a Dog  object named spot  and, using the . operator, set the name  attribute to the string "Spot"  and the age  attribute to 5 .

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"

Dog test_dog();

namespace {
   

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

TEST_F(Evaluate, ExampleTest) {
    Dog t = test_dog();
    EXPECT_EQ("Spot", t.name) << "The dog's name should be Spot";
    EXPECT_EQ(5, t.age) << "The dog's age should be 5";
}

}  // namespace

Solution(s)

Solution 1

#include "Dog.h"

Dog test_dog() {
//---- WRITE YOUR CODE BELOW THIS LINE----

    Dog spot;
    spot.name = "Spot";
    spot.age = 5;

//---- WRITE YOUR CODE ABOVE THIS LINE----
//---- DO NOT CHANGE THE CODE BELOW----
    return spot;
}

Solution 2

//---- DO NOT MODIFY THIS FILE----

#ifndef __DOG_H__
#define __DOG_H__
#include 

class Dog {
public:
    std::string name;
    int age;
};

#endif

Solution 3

//