C_Class_Exercise.pptx
C_Class_Exercise.pptx
Polymorphism
Introduction to Computer Science – C++ Exercise
3
Introduction to Computer Science – C++ Exercise
Example 2
4
Introduction to Computer Science – C++ Exercise
Example 3
5
Introduction to Computer Science – C++ Exercise
Inheritance
class Vehicle {
public:
string brand = “HONDA";
void func() {
cout << “I am HONDA! \n" ;
}
};
6
Introduction to Computer Science – C++ Exercise
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
7
Introduction to Computer Science – C++ Exercise
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class." ;
}
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
} 8
Introduction to Computer Science – C++ Exercise
// Derived class
Public: members of a class class Programmer: public Employee {
public:
are accessible from outside int bonus;
the class void setSalary(int s) {
salary = s;
}
Private: members can only int getSalary() {
be accessed within the class) return salary;
}
Protected: is similar to };
private, but it can also be int main() {
accessed in the inherited Programmer myObj;
class: myObj.setSalary(50000);
myObj.bonus = 15000;
cout << "Salary: " << myObj.getSalary() << "\n";
cout << "Bonus: " << myObj.bonus << "\n";
return 0;
}
9
Introduction to Computer Science – C++ Exercise
Polymorphism
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
} int main() {
}; Animal myAnimal;
// Derived class Pig myPig;
class Pig : public Animal { Dog myDog;
public:
void animalSound() {
cout << "The pig says: wee wee \n"; myAnimal.animalSound();
} myPig.animalSound();
};
myDog.animalSound();
// Derived class return 0;
class Dog : public Animal {
public: }
void animalSound() {
cout << "The dog says: bow wow \n";
}
};
10
C++ Inheritance and
Polymorphism MCQs
Introduction to Computer Science – C++ Exercise
MCQ
12
Introduction to Computer Science – C++ Exercise
MCQ
13
Introduction to Computer Science – C++ Exercise
MCQ
14
Introduction to Computer Science – C++ Exercise
MCQ
15
Introduction to Computer Science – C++ Exercise
MCQ
A. Inaccessible
B. Accessible
C. Protected
D. Public
16
Introduction to Computer Science – C++ Exercise
MCQ
17
Introduction to Computer Science – C++ Exercise
MCQ
18
Introduction to Computer Science – C++ Exercise
MCQ
A. Class Re-usability
B. Creating a hierarchy of classes
C. Extendibility
D. All of the above
19
Introduction to Computer Science – C++ Exercise
20
Introduction to Computer Science – C++ Exercise
a) 1010
b) 1510
c) 1515
d) 5110
21
Introduction to Computer Science – C++ Exercise
22
Introduction to Computer Science – C++ Exercise
23
Introduction to Computer Science – C++ Exercise
24
Introduction to Computer Science – C++ Exercise
25
Introduction to Computer Science – C++ Exercise
A. Compiler Dependent
B. Base1 Base2 Derived
C. Base2 Base1 Derived
D. Compiler Error
26
Introduction to Computer Science – C++ Exercise
27
Introduction to Computer Science – C++ Exercise
28
Introduction to Computer Science – C++ Exercise
A. Base::lfc(int i) called
B. Derived::lfc() called
C. Base::lfc() called
D. Compiler Error
29
Introduction to Computer Science – C++ Exercise
A. In find
B. In course
C. Compiler Error: Ambiguous call to
print()
D. None of the above
30