Data members are variables that hold the data or state of an object created from a class. They represent the properties of the object.
Click to reveal answer
beginner
What are member functions in a C++ class?
Member functions are functions defined inside a class that operate on the data members. They define the behavior or actions of the object.
Click to reveal answer
intermediate
How do you access data members inside member functions?
Inside member functions, you can directly use data members by their names or use the this pointer to refer to the current object's data members.
Click to reveal answer
intermediate
Can member functions modify data members? Explain.
Yes, member functions can modify data members unless the function is declared as <code>const</code>. Non-const member functions can change the object's state.
Click to reveal answer
beginner
What is the difference between public and private data members?
<code>Public</code> data members can be accessed from outside the class, while <code>private</code> data members can only be accessed within the class or by friend functions/classes.
Click to reveal answer
Which of the following is a data member in a class?
Aint age;
Bvoid display();
Cclass Person {}
Dreturn 0;
✗ Incorrect
Data members are variables like int age;. Functions like void display(); are member functions.
What keyword is used to define a member function outside the class?
Aclass
Bnamespace
C:: (scope resolution operator)
Dtemplate
✗ Incorrect
The scope resolution operator :: is used to define member functions outside the class.
Which access specifier restricts access to data members only within the class?
Apublic
Bprivate
Cprotected
Dexternal
✗ Incorrect
private members are accessible only inside the class.
Can a const member function modify data members?
AOnly if data members are mutable
BNo, never
COnly if data members are public
DYes, always
✗ Incorrect
A const member function cannot modify normal data members but can modify those declared mutable.
How do member functions help in object-oriented programming?
AThey store data
BThey create new classes
CThey delete objects
DThey define object behavior
✗ Incorrect
Member functions define what actions or behaviors an object can perform.
Explain what data members and member functions are in a class and how they relate to an object.
Think about how a real-world object has properties and actions.
You got /3 concepts.
Describe the difference between public and private data members and why access control is important.
Consider why some information should be hidden from outside access.
You got /3 concepts.
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
Step 1: Understand what data members are
Data members are variables inside a class that store information about each object created from that class.
Step 2: Differentiate from member functions
Member functions are actions or behaviors, not storage. Keywords and loops are unrelated to data members.
Final Answer:
A variable that holds information about an object -> Option C
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
Step 1: Recall correct member function syntax
Member functions are declared with return type, name, and parentheses, ending with a semicolon inside the class.
Step 2: Check each option
void display(); matches correct syntax: return type 'void', function name 'display', parentheses, and semicolon. Others are invalid syntax.
Final Answer:
void display(); -> Option A
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
Step 1: Understand member function calls
The function setLength(10) sets the data member length to 10 for object box1.
Step 2: Check output from getLength()
The function getLength() returns the current value of length, which was set to 10.
Final Answer:
10 -> Option D
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
Step 1: Check access to data member 'age'
The data member 'age' is private by default because no access specifier is given before it.
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.
Final Answer:
Error: 'age' is private and cannot be accessed directly -> Option A
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
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.
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.
Final Answer:
obj.increment(); -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.