c++ Please explain what these really mean. Next, consider the following advice t
ID: 3677190 • Letter: C
Question
c++
Please explain what these really mean.
Next, consider the following advice that Bjarne Stroustrup, the creator of C++, provided to C++ programmers:
When you program, you create a concrete representation of the ideas in your solution to some problem. Let the structure of the program reflect those ideas as directly as possible:
i. If you can think of “it” as a separate idea, make it a class.
ii. If you can think of “it” as a separate entity, make it an object of some class.
iii. If two classes have a common interface, make that interface an abstract class.
iv. If the implementation of two classes has something significant in common, that commonality is a base class.
v. If a class is a container of objects, make it a template.
vi. If a function implements an algorithm for a container, make it a template function the algorithm for a family of containers.
vii. If a set of classes, templates, etc., are logically related, place them in a common namespace.
Explanation / Answer
A class in C++ is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private).
i. If you can think of “it” as a separate idea, make it a class.
In order to keep the program from getting complicated , we try and put different ideas in different classes in c++.
A particular class will contain a particular idea and all the variables and methods related to that idea only. If you want to implement a different idea , you can make a different class and put all the variable and methods in it.
ii. If you can think of “it” as a separate entity, make it an object of some class.
When we want to call a method of a particular class from some other class, we make the object of that particular class and call it from the other class. It helps in implemeting classes in each other by creating objects. The object will be a separate entity.
iii. If two classes have a common interface, make that interface an abstract class.
In C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data. A class is made abstract by declaring at least one of its functions as pure virtual function. So common classes interface are made adbstract class.
iv. If the implementation of two classes has something significant in common, that commonality is a base class.
Base class represent the classes which can be used by other classes to implement their methods. SO when two classes have many things in commom, in order to make the code more efficient we try and make that commonality a base class.
v. If a class is a container of objects, make it a template.
A class template by itself is not a type, or an object, or any other entity. it is a container of objects. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class
vi. If a function implements an algorithm for a container, make it a template function the algorithm for a family of containers.
Templates are a way of making your classes more abstract by letting you define the behavior of the class without actually knowing what datatype will be handled by the operations of the class. To a large degree, a templated class is more focused on the algorithmic thought rather than the specific nuances of a single datatype. Templates can be used in conjunction with abstract datatypes in order to allow them to handle any type of data.
vii. If a set of classes, templates, etc., are logically related, place them in a common namespace.
This is quite obvious as things which are logically related should pe placed in the same place.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.