OOP - DSWT - CPP
OOP - DSWT - CPP
DSWT – 2 (C++)
11. Private members of a class can only be accessed within the same class.
Answer:
17. Private members of a class can be accessed by anyone outside the class, but not by other members of
the class.
Answer:
18. A child class in c++ can access private members of its parent class directly.
Answer:
19. A method defined in child class overrides the method of parent class if they have same name.
Answer:
20. Overloading methods is implemented by defining multiple methods with the same parameters.
Answer:
Fill in the Blanks
21. It is necessary for every function except ____________ to have a return type, if we don’t want to
return anything from a function it’s return type would be ___________.
Answer:
22. Function ____________ occurs when multiple methods with the same name but different parameters
are used in a class.
Answer:
23. In OOP, the concept of _____________ allows a class to define structure, without providing the
implementation of its functions.
Answer:
25. We can access all _________ members (functions & variables) inside the class and it’s child classes.
Answer:
26. Changing a function in a derived class to provide a new function replacing the base class's function,
is called ________________.
Answer:
27. When creating an object of a class, the auto-call function used to initialize the object is called _____.
Answer:
30. There are ______ Pillars of OOP (Object Oriented Programming) _________, _________,
_________ and __________.
Answer:
Kill the Bug
Find and correct the errors also show the output.
31.
#include <iostream>
using namespace std;
class Car {
public:
string make;
string model;
int main() {
Car car1("Toyota", "Camry");
cout << car1.make << endl;
return 0;
}
Answer:
Output:
32.
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
int main() {
cout << calc.add(2, 3) << endl;
cout << calc.add(2, 3, 4) << endl;
return 0;
}
Answer:
Output:
33.
#include <iostream>
using namespace std;
class Person {
public:
string name;
Constructor(string name) {
this->name = name;
}
};
int main() {
Person p("Alice");
cout << p.name << endl;
return 0;
}
Answer:
Output:
34.
#include <iostream>
using namespace std;
class Animal {
public:
string sound;
Animal(string sound) {
this->sound = sound;
}
virtual void speak() {
cout << sound << " makes a sound" << endl;
}
};
int main() {
Dog dog("Woof");
dog.speak();
return 0;
}
Answer:
Output:
35.
#include <iostream>
using namespace std;
class Car {
public:
string make;
string model;
Car(string make, string model) {
this->make = make;
this->model = model;
}
string display() {
return "Car make: " + make + ", Model: " + model;
}
};
int main() {
Car car("Toyota");
cout << car.display() << endl;
return 0;
}
Answer:
Output: