Object interaction means how different objects in a program communicate or work together by calling each other's methods or accessing data.
Click to reveal answer
beginner
How does one object call a method of another object in C++?
One object uses the dot operator (.) with the other object's name to call its method, like objectName.methodName().
Click to reveal answer
beginner
Why is object interaction important in programming?
It helps break complex problems into smaller parts where objects work together, making code easier to understand and maintain.
Click to reveal answer
beginner
What is an example of object interaction in real life?
Think of a car object and an engine object. The car object can tell the engine object to start, showing interaction.
Click to reveal answer
intermediate
Can objects share data directly in C++?
Objects usually share data by calling each other's methods, not by directly accessing private data. This keeps data safe and organized.
Click to reveal answer
How does one object call a method of another object in C++?
AUsing the dot operator (.)
BUsing the arrow operator (->) without pointers
CBy copying the entire object
DBy declaring a new object inside the method
✗ Incorrect
In C++, objects call other objects' methods using the dot operator, like object.method().
What is the main benefit of object interaction?
AIt avoids using methods
BIt makes code harder to read
CIt breaks problems into smaller parts
DIt copies all data between objects
✗ Incorrect
Object interaction helps divide complex problems into smaller, manageable parts.
Which of these is a real-life example of object interaction?
AA car object copying an engine object
BA car object ignoring the engine object
CAn engine object deleting a car object
DA car object telling an engine object to start
✗ Incorrect
The car telling the engine to start is a good example of objects working together.
Can objects directly access each other's private data in C++?
AYes, always
BNo, they use methods to share data
COnly if they are the same class
DOnly if they are pointers
✗ Incorrect
Objects use methods to share data to keep data safe and organized.
Which operator is used to access members of an object pointer in C++?
A-> (arrow)
B:: (scope resolution)
C. (dot)
D* (dereference)
✗ Incorrect
The arrow operator (->) is used to access members of an object pointer.
Explain how two objects can interact in C++ with a simple example.
Think about how one object can tell another to do something.
You got /3 concepts.
Why is it better for objects to share data through methods rather than directly accessing each other's data?
Consider how private data protects the object.
You got /3 concepts.
Practice
(1/5)
1. What does it mean when one object calls a method on another object in C++?
easy
A. The first object is asking the second object to perform an action.
B. The first object copies the second object's data.
C. The first object deletes the second object.
D. The first object creates a new object inside the second object.
Solution
Step 1: Understand method calls between objects
When one object calls a method on another, it requests that object to do something or provide information.
Step 2: Differentiate from other actions
Copying data, deleting, or creating objects are different operations, not the same as calling a method.
Final Answer:
The first object is asking the second object to perform an action. -> Option A
Quick Check:
Object method call = request action [OK]
Hint: Method call means asking another object to do something [OK]
Common Mistakes:
Confusing method call with copying data
Thinking method call deletes objects
Assuming method call creates new objects inside others
2. Which of the following is the correct syntax to pass an object obj of class Car to a method drive of another object driver?
easy
A. driver.drive(obj);
B. drive.driver(obj);
C. driver->drive(obj);
D. drive(obj).driver;
Solution
Step 1: Identify correct method call syntax
For an object driver, calling method drive with argument obj uses dot notation: driver.drive(obj);
Step 2: Eliminate incorrect syntax
drive.driver(obj); reverses object and method, driver->drive(obj); is for pointers, and drive(obj).driver; is invalid.
Final Answer:
driver.drive(obj); -> Option A
Quick Check:
Object.method(argument) = driver.drive(obj) [OK]
Hint: Use object.method(argument) syntax for calling methods [OK]
Common Mistakes:
Using arrow operator on non-pointer objects
Swapping method and object names
Incorrect punctuation or order
3. What is the output of this code?
#include <iostream>
class Light {
public:
void turnOn() { std::cout << "Light is ON" << std::endl; }
};
class Switch {
public:
void press(Light &l) { l.turnOn(); }
};
int main() {
Light roomLight;
Switch wallSwitch;
wallSwitch.press(roomLight);
return 0;
}
medium
A. Compilation error
B. Light is OFF
C. No output
D. Light is ON
Solution
Step 1: Understand method call inside press()
The press method calls turnOn() on the passed Light object, which prints "Light is ON".
Step 2: Trace main function calls
wallSwitch.press(roomLight); calls turnOn() on roomLight, so output is "Light is ON".
Final Answer:
Light is ON -> Option D
Quick Check:
Method call prints "Light is ON" [OK]
Hint: Method calls inside other methods print output [OK]
Common Mistakes:
Assuming no output because press() returns void
Confusing reference passing with copying
Thinking code causes compilation error
4. Identify the error in this code snippet:
#include <iostream>
class Engine {
public:
void start() { std::cout << "Engine started" << std::endl; }
};
class Car {
public:
void ignite(Engine e) { e.start(); }
};
int main() {
Engine myEngine;
Car myCar;
myCar.ignite(myEngine);
return 0;
}
medium
A. Method start() is not declared correctly.
B. Car class should inherit from Engine.
C. Passing Engine by value causes unnecessary copying.
D. Cannot call start() on Engine object.
Solution
Step 1: Analyze parameter passing in ignite()
The method ignite takes Engine e by value, which copies the object unnecessarily.
Step 2: Suggest improvement
Passing by reference (Engine &e) avoids copying and is more efficient.
Final Answer:
Passing Engine by value causes unnecessary copying. -> Option C
Quick Check:
Pass objects by reference to avoid copying [OK]
Hint: Pass objects by reference to avoid copies [OK]
Common Mistakes:
Thinking method start() is incorrect
Assuming inheritance is required
Believing start() cannot be called on Engine
5. Given classes Teacher and Student, how can you design a method in Teacher that lets a teacher assign a grade to a student object? Choose the best approach.
hard
A. Make Student inherit from Teacher and override a grade method.
B. Define a method in Teacher that takes a Student reference and an int grade, then calls a Student method to set the grade.
C. Store grades only inside Teacher without involving Student objects.
D. Use global variables to store grades for all students.
Solution
Step 1: Understand object interaction for assigning grades
The Teacher should interact with Student by calling a method on Student to set the grade, passing the grade value.
Step 2: Evaluate options
Define a method in Teacher that takes a Student reference and an int grade, then calls a Student method to set the grade. correctly uses a method in Teacher that takes a Student reference and grade, then calls Student's method to set the grade. Other options misuse inheritance, ignore object interaction, or use bad global state.
Final Answer:
Define a method in Teacher that takes a Student reference and an int grade, then calls a Student method to set the grade. -> Option B
Quick Check:
Teacher calls Student method to assign grade [OK]
Hint: Teacher calls Student method with grade parameter [OK]
Common Mistakes:
Misusing inheritance for unrelated roles
Ignoring encapsulation by using globals
Storing grades only in Teacher without Student update
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.