System Level Design Languages:Basic Concepts Used in C++
Basic Concepts Used in C++
As stated above, C++ can be considered as a superset of the C language. Since it is even more complicated than C, we cannot discuss this language in detail. Therefore, we will simply describe a few object-oriented concepts used in C++. References to several C++ texts appear at the end of this chapter. Referring to code of Figure 86.40 facilitates understanding of the discussion that follows. More complete C and C++ examples can be found at the end of Sections 86.3 and 86.4, and Chapter 89.
• Class: A class is a user-defined type (like sample in line 1 of Figure 86.40). It can be realized as an item in the real world. It has characteristics and it can perform different tasks. For example, a class can describe a human being, a chair, a vehicle, a CPU, a cat, etc. Everything with a specified functionality and characteristics can be modeled with a class.
Classes have attributes (like name_index in Figure 86.40) and methods (like AddName in Figure 86.40). Their characteristics are described with attributes, while their operations and tasks are described by methods. Attributes are implemented with variables, while methods are implemented by functions inside the class definition.
An attribute and a method can be of three types: private, public, or protected. All other classes can access public methods or variables of a class. In contrast, no other class can access a private method and variables of a class. Private members can only be accessed in other methods of that class. Accessibility of the protected attributes and methods (members) are discussed later in this section.
Unlike a normal variable in C/C++, class attributes cannot be initialized in the class body. They can be initialized in a special method called constructor (line 3 in Figure 86.40). Attributes can be initialized in other methods, but it is recommended to initialize them in the constructor. Every class has a constructor and a destructor (line 4 in Figure 86.40). In the constructor, which must have the same name as its class, all necessary initialization and memory allocations are performed (line 14 to line 17 in Figure 86.40). A constructor is executed automatically when a class is created (if defined by reference) or allocated (if defined as a pointer). On the contrary, a destructor, the name of which begins with ~ followed by its class name, is executed automatically just before an instance of a class is destroyed. The necessary memory de-allocations are usually done in the destructor.
Methods in a class are usually developed outside the class definition. In this case, each method definition in a class plays the role of a prototype for that function (line 5, line 6, and line 8 in Figure 86.40). In the method implementation (line 14, line 19, and line 23 in Figure 86.40), the name of the class followed by two colons must come before the function (method) name. However, the function implementation can be enclosed in the class definition. This is not recommended unless the method body is very small. A class and one of its methods’ implemen- tation are shown in Figure 86.41.
• Generalization: In the C language, classes can be derived from other classes. Consider a case where class A is derived from class B. In this case, B is called the parent of A, and A is considered the child of B. For example, in Figure 86.40, parent_cls is the parent of sample. The public and protected attributes and methods (members) of the parent class are inherited automatically to the child class. Here, we can define a protected member of a class. A protected member acts like a public member for its children and acts like a private member otherwise. Multiple inheritance is when a class has more than one parent. A class can also have several children classes.
As an example of classes, consider a class for electrical devices (named ElecDev). It is obvious that there are several electrical devices (like televisions, refrigerators, computers, and lamps). All of these devices have a number of common characteristics like the voltage they work with, their power dissipation, the electrical current they use, etc. These general characteristics can be defined in the ElecDev class, while each of them has its own special characteristics. These special characteristics can be defined as a separate class derived from the ElecDev class. Figure 86.42 shows the electrical devices class hierarchy.
• Friend members: Function f can access and use class A’s private members if f is defined as a friend for A. It is generally recommended not to use friend members except for the case when a global variable or public data members need to be used. A friend method is defined by the friend keyword before the method definition.
• Object: An object is a class instance (like obj in line 12 of Figure 86.40). Class members can be used in a program through objects. The relationship between a class and an object is equivalent to the relationship between a data type and a variable. Therefore, similar to variables, an object can be defined statically (as a reference) or dynamically (as a pointer). When an object is defined statically, its constructor is executed after its definition and its destructor is executed before the program exits the current function. If an object is defined by a pointer to a class, the programmer is responsible for its memory allocation and deallocation. Memory for a pointer object is allocated by the new function as pointed out earlier. The constructor is executed after calling the new function. A pointer memory is destroyed by calling the delete function. The destructor of that class is similarly called before deleting this object.
• Namespace: Sets of elements in C++ (such as classes, variables, functions, etc.) that have a logical relationship with each other can be put in a namespace. All elements inside a namespace must have unique names. To reference an element inside a namespace, we need to use that namespace first (by writing ‘using namespace_name’) and then use the name of that namespace followed by two colons together with the element that is intended to be accessed. Namespaces become useful for large programs.
• Template: Templates support generic programming. Using templates gives programmers the opportunity to use different data types as arguments. Suppose we want to develop a stack class. This stack must store many types of data. One way is to define a stack class and overload every method in this class for every needed data type. An alternative, more efficient method is to use templates. We can define a template class for stack and define the data type it stores, and the data of that type as arguments of the template. Then this template class can be instantiated with any type and its methods and attributes can be used in codes. Figure 86.43 presents a template class for a general stack. For more details on templates you can refer to Ref. [4].
Comments
Post a Comment