PF Practice
PF Practice
💡 Pointers
Answer: b) *
🧰 Function Overloading
🌀 Polymorphism
Answer: c) virtual
Answer: c) Compilation
16. Which concept allows reusing code from existing classes?
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction
Answer: b) Inheritance
Answer: b) private
Answer: b) protected
20. Which concept hides internal details and shows only essential
features?
a) Inheritance
b) Encapsulation
c) Abstraction
d) Polymorphism
Answer: c) Abstraction
🔗 Inheritance
Answer: a) Public and protected members of the base class become private
in derived class
🧬 Polymorphism
Answer: b) override
Answer: b) Base class and derived class functions with same name and
signature
Answer: b) new
Answer: b) No
📦 Miscellaneous OOP
Answer: c) Abstraction
cpp
CopyEdit
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "Base Constructor\n"; }
~Base() { cout << "Base Destructor\n"; }
};
int main() {
Derived d;
return 0;
}
Dry Run:
• Derived d; triggers:
o Base Constructor
o Derived Constructor
• Upon exiting main():
o Derived Destructor
o Base Destructor
Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { cout << "Animal
sound\n"; }
};
int main() {
Animal *a;
Dog d;
a = &d;
a->sound();
return 0;
}
Dry Run:
Output:
Dog barks
#include <iostream>
#include <cstring>
using namespace std;
class Student {
char *name;
public:
Student(const char *n) {
name = new char[strlen(n) + 1];
strcpy(name, n);
}
void print() {
cout << name << endl;
}
~Student() {
delete[] name;
}
};
int main() {
Student s1("John");
Student s2 = s1; // Deep copy
s2.changeName("Alice");
s1.print();
s2.print();
return 0;
}
Dry Run:
Output:
John
Alice
Instructions:
#include <iostream>
using namespace std;
class Base {
public:
void display(int x) { cout << "Base display: " <<
x << endl; }
};
int main() {
Derived d;
d.display(5);
d.display(5.5);
return 0;
}
Output:
Derived display: 5
Derived display: 5.5
2. Object Slicing in Assignment
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() { cout << "Base print" <<
endl; }
};
int main() {
Derived d;
Base b = d; // Object slicing
b.print();
return 0;
}
Output:
Base print
3. Abstract Class and Pure Virtual Function
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0;
};
int main() {
Shape* s = new Circle();
s->draw();
delete s;
return 0;
}
Output:
Drawing Circle
#include <iostream>
#include <cstring>
using namespace std;
class Book {
char *title;
public:
Book(const char *t) {
title = new char[strlen(t) + 1];
strcpy(title, t);
}
~Book() {
delete[] title;
}
};
int main() {
Book b1("C++ Programming");
Book b2 = b1; // Copy constructor
Book b3("Temp");
b3 = b1; // Assignment operator
b1.show(); b2.show(); b3.show();
return 0;
}
Output:
C++ Programming
C++ Programming
C++ Programming
#include <iostream>
using namespace std;
class Counter {
private:
static int count;
public:
Counter() { count++; }
static void showCount() {
cout << "Count: " << count << endl;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
Counter::showCount();
return 0;
}
Output:
Count: 3