CodeBlanks Logo Icon CodeBlanks

Summary - Classes

Combine member variables, functions and access modifiers to write a User class.

Summary - Classes

In this section you saw the fundamentals of C++ classes. A class is defined with the class keyword and needs a semicolon after the body. Member variables hold data; member functions hold behavior.

The this pointer refers to the calling object; it resolves name clashes and enables chaining. Access modifiers (public, private, protected) control external access to members and enable encapsulation. Now we will consolidate all of this with a task.

Practice Define the Light class and use it

Define a Light class. int watts; member (private), under public: add void setWatts(int watts) { this->watts = watts; } and int getWatts() { return watts; }. In main create an instance, call setWatts(60) and print the result of getWatts() with std::cout.

Starter code

main.cpp
#include <iostream>

// Define the Light class here

int main() {
    // Create a Light instance, call setWatts and print getWatts
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Light {
    int watts;
public:
    void setWatts(int watts) {
        this->watts = watts;
    }
    int getWatts() {
        return watts;
    }
};

int main() {
    Light bulb;
    bulb.setWatts(60);
    std::cout << bulb.getWatts();
    return 0;
}

Expected output

60

Practice Add a member function to the Light class

Take the Light class above and add a new member function void describe(). Its body should print std::cout << watts << " watts";. In main create an instance, call setWatts(40) and describe().

Starter code

main.cpp
#include <iostream>

class Light {
    int watts;
public:
    void setWatts(int watts) {
        this->watts = watts;
    }
    int getWatts() {
        return watts;
    }
    // Add the describe member function here
};

int main() {
    // Create a Light instance, call setWatts and describe
    return 0;
}

Sample solution

main.cpp
#include <iostream>

class Light {
    int watts;
public:
    void setWatts(int watts) {
        this->watts = watts;
    }
    int getWatts() {
        return watts;
    }
    void describe() {
        std::cout << watts << " watts";
    }
};

int main() {
    Light bulb;
    bulb.setWatts(40);
    bulb.describe();
    return 0;
}

Expected output

40 watts

What's next?

You learned the fundamentals of classes: definition, member variables, member functions, the this pointer and access modifiers. When you create an object, initializing members is still manual; this is tedious and error-prone.

In the next section you will learn to initialize objects automatically with a constructor. A constructor is a special member function with the same name as its class, called automatically when an object is declared. This way initial values live in one place.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In class Light { int watts; public: int getWatts(); };, what is the access level of watts?

main.cpp
class Light { int watts; public: int getWatts(); };
  • private Correct answer
  • public
  • protected
  • No access modifier

Explanation: Members without a modifier are treated as private.

2 Complete the assignment with this

Question
void setWatts(int watts) { ____->watts = watts; }
Answer
void setWatts(int watts) { this->watts = watts; }

Explanation: this->watts is the member, watts is the parameter.

All lessons in Intermediate