Bird
Raised Fist0
C++programming~10 mins

Data members and member functions in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Data members and member functions
Create Object
↓
Allocate Data Members
↓
Call Member Function
↓
Access/Modify Data Members
↓
Return or Output Result
↓
End
This flow shows how an object is created with data members, then member functions are called to access or change those data members, and finally results are returned or output.
Execution Sample
C++
class Box {
  int length;
public:
  void setLength(int l) { length = l; }
  int getLength() { return length; }
};
Defines a class Box with a data member length and member functions to set and get its value.
Execution Table
StepActionData Member 'length'Output
1Create object b of class Boxundefined (not set)
2Call b.setLength(10)length = 10
3Call b.getLength()length = 1010
4End of programlength = 10
💡 Program ends after getting length value 10 from object b
Variable Tracker
VariableStartAfter Step 2After Step 3Final
length (data member)undefined101010
Key Moments - 2 Insights
Why is 'length' undefined before calling setLength?
Because data members are not automatically initialized; setLength assigns the value (see execution_table step 2).
How does getLength access the data member?
getLength returns the current value of 'length' stored in the object (see execution_table step 3 output).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'length' after step 1?
Aundefined
B10
C0
DNot declared
💡 Hint
Check the 'Data Member length' column at step 1 in the execution_table.
At which step does 'length' get assigned the value 10?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Data Member length' columns in the execution_table.
If we call getLength() before setLength(), what would be the output?
A10
Bundefined or garbage value
C0
DCompilation error
💡 Hint
Refer to variable_tracker and understand that 'length' is not initialized before setLength is called.
Concept Snapshot
class ClassName {
  data members (variables);
public:
  member functions to access/modify data members;
};
Create object, then use member functions to work with data members.
Full Transcript
This example shows a class named Box with a data member called length. When we create an object b of Box, length is not set yet. We call setLength(10) to assign 10 to length. Then getLength() returns the stored value 10. Data members hold the object's data, and member functions let us safely access or change that data. Without calling setLength first, length would be undefined. This step-by-step trace helps understand how data members and member functions work together in C++.

Practice

(1/5)
1. Which of the following best describes a data member in a C++ class?
easy
A. A keyword used to define a class
B. A function that performs actions on an object
C. A variable that holds information about an object
D. A special type of loop inside a class

Solution

  1. Step 1: Understand what data members are

    Data members are variables inside a class that store information about each object created from that class.
  2. Step 2: Differentiate from member functions

    Member functions are actions or behaviors, not storage. Keywords and loops are unrelated to data members.
  3. Final Answer:

    A variable that holds information about an object -> Option C
  4. Quick Check:

    Data member = variable storing object info [OK]
Hint: Data members store data, member functions do actions [OK]
Common Mistakes:
  • Confusing data members with member functions
  • Thinking data members are keywords
  • Mixing data members with control structures
2. Which of the following is the correct way to declare a member function named display inside a C++ class?
easy
A. void display();
B. display void();
C. void display {}
D. function void display();

Solution

  1. Step 1: Recall correct member function syntax

    Member functions are declared with return type, name, and parentheses, ending with a semicolon inside the class.
  2. Step 2: Check each option

    void display(); matches correct syntax: return type 'void', function name 'display', parentheses, and semicolon. Others are invalid syntax.
  3. Final Answer:

    void display(); -> Option A
  4. Quick Check:

    Correct function declaration = void display(); [OK]
Hint: Member functions: returnType name(); inside class [OK]
Common Mistakes:
  • Omitting parentheses after function name
  • Using wrong order of return type and name
  • Missing semicolon after declaration
3. What will be the output of the following C++ code?
#include <iostream>
class Box {
public:
  int length;
  void setLength(int len) {
    length = len;
  }
  int getLength() {
    return length;
  }
};

int main() {
  Box box1;
  box1.setLength(10);
  std::cout << box1.getLength();
  return 0;
}
medium
A. 0
B. Compilation error
C. length
D. 10

Solution

  1. Step 1: Understand member function calls

    The function setLength(10) sets the data member length to 10 for object box1.
  2. Step 2: Check output from getLength()

    The function getLength() returns the current value of length, which was set to 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Set length to 10, get length returns 10 [OK]
Hint: Setter sets value, getter returns it [OK]
Common Mistakes:
  • Assuming default value 0 is printed
  • Confusing function names
  • Expecting compilation error due to missing constructor
4. Identify the error in the following C++ class code:
#include <iostream>
class Person {
  int age;
public:
  void setAge(int a) {
    age = a;
  }
  int getAge() {
    return age;
  }
};

int main() {
  Person p;
  p.age = 25;
  std::cout << p.getAge();
  return 0;
}
medium
A. Error: 'age' is private and cannot be accessed directly
B. No error, output is 25
C. Error: Missing return type for getAge
D. Error: setAge function is not called

Solution

  1. Step 1: Check access to data member 'age'

    The data member 'age' is private by default because no access specifier is given before it.
  2. Step 2: Analyze main function usage

    In main, code tries to access 'p.age = 25;' directly, which is not allowed for private members, causing a compilation error.
  3. Final Answer:

    Error: 'age' is private and cannot be accessed directly -> Option A
  4. Quick Check:

    Private members can't be accessed outside class [OK]
Hint: Private members need public functions to access [OK]
Common Mistakes:
  • Trying to access private data members directly
  • Ignoring default private access in classes
  • Assuming no error if setter is not called
5. Given the class below, which code snippet correctly increases the count data member by 1 for an object obj?
class Counter {
private:
  int count = 0;
public:
  void increment() {
    count++;
  }
};
hard
A. obj.count = obj.count + 1;
B. obj.increment();
C. Counter::increment();
D. obj.count++; obj.increment();

Solution

  1. Step 1: Understand the class design

    The class has a private data member count = 0 and provides a public member function increment() that increases count by 1.
  2. Step 2: Check each option for correctness

    obj.count = obj.count + 1; tries to access private count directly, compilation error. obj.increment(); calls the member function correctly. Counter::increment(); tries to call non-static member function without an object, invalid. obj.count++; obj.increment(); first accesses private count, error.
  3. Final Answer:

    obj.increment(); -> Option B
  4. Quick Check:

    Use member function to change data member [OK]
Hint: Use object's member function to update data members [OK]
Common Mistakes:
  • Calling member function without object
  • Manually changing data member instead of using function
  • Incrementing count twice accidentally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard