C++ (4) 10. (6 points) 3) Given the code fragment below, each occurrence of cons
ID: 3918211 • Letter: C
Question
C++
(4)
10. (6 points) 3) Given the code fragment below, each occurrence of const is a promise to the compiler that the compiler will enforce. For each of the three cases, explain what the promise enforces? (Label your answers and be specific) class MyClass i public: MyClass MyClass(int n): const Myclass operator a(const Myclassé right) const: 2A private: int i; 11. (2 points) When a class that we create involves pointers and/or dynamic memory, we typi cally have to overload the assignment operator. It is one member of what is referred to as the "Big Three." What are the other two members? 12. (4 points) Briefly describe how C++ templates support algorithm abstraction (2 sentences, max)Explanation / Answer
12)Answer:
When we say we are using algorithm abstraction, we mean that we are expressing our algorithms in a very general way so that we can ignore incidental detail and concentrate on the substantive part of the algorithm. Function templates are one feature of C++ that supports algorithm abstraction
Abstraction is interessant only if you want to do polymorphism, or to prevent the instantiation of a specific class, otherwise it's more appropriate to use simple inheritance.
To do polymorphism you need a pointer or a reference to an object, and you need a virtual method.
If those conditions are satisfied you can enjoy all the advantages of abstraction and polymorphism.
Steps:
1- Create an abstract class
2- Create a virtual method possibly without implementation.
3- Create one or more derived classes from the abstract class
4- Implement the virtual method in all derived classes (it's important if the abstract class don't give an implementation).
5- Create as many instantiations as you want of derieved (concrete) classes (you can create a list of the abstract class pointers, and each pointer will reference a concrete instantiation).
6- Create an abstract class pointer (you can not instantiate the abstract class) and reference a concrete instantiation. (or you can just iterate over a list if you created it before)
7- Call the virtual method using the abstract class pointer.
Result:
You will have different results depending on the object you are referencing.
Notes:
If the method is not virtual, it will not work. you will get the result of the abstract class implementation even if you are referencing an instantiation of another class.
If you don't use pointers it will not work, because it will not be possible to know the referenced object class type.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.