40 Lecture
CS201
Midterm & Final Term Short Notes
Objects as Class Members
In C++, you can define objects as class members. This is known as composition, and it allows you to create complex data structures by combining simpler objects. Objects as class members can be used to implement relationships such as "has-a" or "
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is composition in C++? A. The process of defining classes B. The process of defining objects as class members C. The process of creating pointers D. The process of using inheritance
Answer: B
- What is the purpose of defining objects as class members? A. To create complex data structures B. To access private data members of another class C. To define a new data type D. To implement inheritance
Answer: A
- How are objects as class members constructed and destructed? A. They are never constructed or destructed B. They are constructed and destructed along with the parent object C. They are constructed and destructed separately from the parent object D. They are only destructed when the parent object is destructed
Answer: B
- How do you access an object that is a member of a class? A. Using the -> operator B. Using the :: operator C. Using the dot (.) operator D. Using the * operator
Answer: C
- Which relationship can be implemented using objects as class members? A. "is-a" B. "has-a" C. "inherits-from" D. "contains-a"
Answer: B
- Can an object be a member of more than one class? A. Yes, but only if both classes are derived from the same base class B. No, an object can only be a member of one class C. Yes, an object can be a member of any number of classes D. Yes, but only if the classes are in the same namespace
Answer: B
- What happens if you try to assign one object to another object that is a member of a class? A. The program crashes B. A compiler error is generated C. The object is copied to the new object D. The object is deleted and a new object is created
Answer: C
- Can you use objects as class members in a union? A. Yes, but only if the objects are of the same type B. No, objects cannot be used as class members in a union C. Yes, objects can be used as class members in a union D. Yes, but only if the union is in a namespace
Answer: B
- Can you use pointers to access objects that are members of a class? A. Yes, but only if the objects are public B. Yes, but only if the objects are static C. No, pointers cannot be used to access objects that are members of a class D. Yes, you can use pointers to access objects that are members of a class
Answer: D
- How do you initialize an object that is a member of a class? A. Using the new operator B. Using the sizeof operator C. Using a constructor D. Using a destructor
Answer: C
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the meaning of "Composition" in OOP? Ans: Composition is the process of including one class inside another class as a member variable.
What are the advantages of using objects as class members? Ans: The advantages of using objects as class members are reusability, encapsulation, and modularity.
What is the difference between composition and inheritance? Ans: Composition is a "has-a" relationship, while inheritance is an "is-a" relationship. In composition, one class is a member of another class, while in inheritance, one class inherits the properties and behaviors of another class.
How do you access the members of an object inside another object? Ans: You can access the members of an object inside another object by using the dot operator.
What is the role of constructors in objects as class members? Ans: Constructors are used to initialize the objects as class members.
Can we have an object of the same class as a member variable? Ans: Yes, we can have an object of the same class as a member variable. This is called "recursion."
What is the purpose of using objects as class members? Ans: The purpose of using objects as class members is to create more complex classes with better functionality and modularity.
What is the meaning of aggregation in OOP? Ans: Aggregation is the process of including one class inside another class as a member variable, but the included class can exist independently of the containing class.
How do you create an object as a member variable of another object? Ans: To create an object as a member variable of another object, you must declare a member variable of the desired class inside the containing class.
What are the disadvantages of using objects as class members? Ans: The main disadvantage of using objects as class members is that it can increase the complexity of the program and make it harder to understand and maintain. It can also lead to excessive memory usage if not managed properly.