Mithun Jadhav | Connecting



Structured Language Question Bank for C++– 2016

1. Data Types

A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are: integer, floating point unit number, character, string, and pointer. Usually, a limited number of such data types come built into a language. The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored.

With object-oriented programming, a programmer can create new data types to meet application needs. Such an exercise as known as "data abstraction" and the result is a new class of data. Such a class can draw upon the "built-in" data types such as number integers and characters. For example, a class could be created that would abstract the characteristics of a purchase order. The purchase order data type would contain the more basic data types of numbers and characters and could also include other object defined by another class. The purchase order data type would have all of the inherent services that a programming language provided to its built-in data types.

Data Types

Programming uses a number of different data types. A data type determines what type of values an object can have and what operations can be performed.

Strings

One of the most widely used data types is a string. A string consists of one or more characters, which can include letters, numbers, or other types of characters. You can think of a string as plain text.

A string represents alphanumeric data. This means that a string can contain many different characters, but they are all considered as if they were text, even if the characters are numbers. A string can also contain spaces. This presents a bit of an issue. How are you going to distinguish between the value of a string and the actual code of the program? The solution is to mark the beginning and end of a string with a special character, typically a quote. For example, the following code is used to print text to the screen:

print 'Hello World!'

The use of quotes ensures that the text 'Hello World!' is recognized as a string and not as two separate words that may have some special meaning in the programming language. The use of quotes also makes it possible to use numeric characters as part of a string. For example, the following code is used to store a street address:

address = '123 Central Avenue'

Without the quotes, the numeric characters 123 would be interpreted as a number, but with the quotes, it is recognized as being part of a string that represents a street address.

Numeric Data Types

The second most important data type is numeric data. There are several different ones.

An integer is a numeric value without a decimal. Integers are whole numbers and can be positive or negative. Sometimes a distinction is made between short and long integers, referring to how much data storage is used for the number. A short integer is typically stored using 16 bits, which means you can store up to 2^16, or 65,536, unique values. For any numbers larger than that, you would need to use a long integer, which uses 32 bits or more.

A number with a decimal is referred to as a decimal, a float or a double. The terminology varies somewhat with the programming language being used. The term 'float' comes from floating point, which means you can control where the decimal point is located. The term 'double' refers to using double the amount of storage relative to a float. Working with numbers in code is a little bit like using a calculator. Here is an example of how numeric values are used in code, in this case using a multiplication character:

result = 3 * 117.89

The value stored in the 'result' would be 353.67.

Boolean Data

The Boolean data type can only represent two values: true or false. Typically, a 1 is used to represent True, and a 0 is used to represent False. Consider the following example where a user inputs two values and the program determines whether the first one is smaller than the second one or not.

x = 8

y = 7

xy

In this example the first value is in fact not smaller than the second one, and the program therefore results in a Boolean value of False. The Boolean type is the primary results of conditional statements, which are used to control workflow in program. For example, if a particular condition is true, then do this - if the condition is false, then do something else.

Composite Data Types

The data types covered so far are often referred to as primitive data types. A composite data type is obtained by combining more than one primitive data type. These are also referred to as data structures. Common examples of composite data types are lists and arrays.

A list contains elements of one particular data type. For example, a list could contain strings. An example would be the names of all players on a soccer team. Each name is a string, but when you organize all the names together, they form a list. A list is the simplest data structure.

For example, a list of strings could look like this:

('John', 'Paul', 'George', 'Ringo')

2. Arrays, functions and pointers

Refer to SL solutions

For Arrays ( Page 65

For Functions ( Page 56

For Pointers ( Page 53

-------------------------------------------------------------------------------------------------

3. Characteristics of OOPS

Principle concepts of OOPs are:

• Abstraction

• Encapsulation

• Inheritance

• Polymorphism

Abstraction is the process of identifying the key aspects of an entity and ignoring the rest. Only those aspects are selected that are important to the current problem scenario.

For eg. Consider a person object, the abstraction of this object would be different in different problem domains.

As demonstrated in the table below, a person would be abstracted in different ways in 3 different problem domains:

|Social Survey |Health Care |Employment |

|Name |Name |Name |

|Age |Age |Age |

|Marital Status |- |- |

|Religion |- |- |

|Income Group |- |- |

|Address |Address |Address |

|Occupation |Occupation |Occupation |

|- |Blood Group |- |

|- |Weight |- |

|- |Previous Record |- |

|- |- |Qualification |

|- |- |Department |

Encapsulation is a mechanism used to hide the data, internal structure and implementation details of an object.

All interaction with the object is through a public interface of operations.

The user knows only about the interface, any changes to the implementation doesnot affect the user. For eg. The internal components of a television and working are hidden from the end user. TV has a set of buttons either on the TV or remote which acts as an interface to operate the TV. Data hiding ensures security. Encapsulation separates interface of an abstraction and its implementation. One advantage is that even if the method/function implementation is changed, the user need not know about it as long as the interface (calling mechanism) remains same.

Inheritance is a mechanism by which a new class is derived from an existing one. It not only helps to reuse the old code but aids in extending the software. Inheritance is similar to parent-child relationship. In inheritance each child has an ‘is-a’ relationship with its parents. The new class which is being created is called as derived class and the class from which it is derived is called as a base class. With the help of inheritance a hierarchy of classes is created.

Generalization and specialization are two points of view that are based on class hierarchies. They express the direction in which the hierarchy is extended.

Generalization: Moving up in the hierarchy is called as generalization. Factoring out common elements within a set of categories into a more general category called super-class. Requires good skills of abstraction.

Specialization: While moving down the hierarchy more and more specific features are added in each sub category that is formed. This is said to be specialization. Allows to capture specific features of a se of objects.

Features of Inheritance:

• Object oriented programming extends abstract data types to allow for type/subtype relationships. It is achieved through inheritance

• To inherit means to recieve properties of an already exisitng class

• When an existing class is inheritted, the derived class inherits all data members as well as the member methods from base class. But not all of them can be accessed by member methods of deived class. The accessibility of base class members in the derived class depends on their access specifiers.

Advantages of inheritance:

• Reusability: Once a class is written and tested, it can be further used for creating new classes. These derived classes not only inherit the features of their base class, but also have their own individualistic features. This means that if the derived class wants to use its base class properties it can do so because these properties are also available to the derived class by virtue of inheritance.

• Extensibility: It is the mechanism of being able to derive classes from existing classes. This provides extensibility of adding and removing classes in a hierarchy as and when required.

Polymorphism is the ability of different types of related objects to respond to the same message in their own ways. The word “polymorphism” is combined from two words, “poly” means many and “morph” is a form. Thus polymorphism is the ability to take more than one form. Polymorphism plays an important role is allowing different objects to share the same external interface although the implementation may be different. Polymorphism a static one or dynamic one dependending on process of associating a function call to an object.

Static polymorphism: It is achieved using early binding. When binding occurs at compile time, it is known as early binding. The argument passed to the method decides the method to be invoked at compile time. Early binding is achieved using method overloading.

Dynamic polymorphism: It is achieved using late binding. When binding process occurs at run time, it is called late binding. The appropriate method is invoked at run time by checking type of an object. Late binding is achieved using method overriding.

------------------------------------------------------------------------------------------------------

4. Difference between call by reference, call by value, call by address

Refer SL solutions notes

For Call by reference (Pg 37

For Call by value ( Pg 36

C++ supports functions to be called by value, call by address / pointer and call by reference.

Call by value in C++:

In call by value, value of variable is passed to the function.

Example of call by value in C++:

void func(int val)

{ ...

}

int i=10;

func(i);

Changes to val will not be reflected to i .

 

Call by Address / Call by pointer in C++:

In call by address or pointer, address of the variable is passed to the function.

Example of call by address in C++:

void func(char *ptr)

{...}

char arr[]="hello";

func(arr); // or func(&arr);

Changes done by ptr will be reflected to arr .

 

Call by Reference in C++:

In call by reference, reference variable is used at the function's formal argument.

Example of call by Reference in C++:

void func(int &ref)

{...}

int i=10;

func(i);

Changes to ref will be reflected to i .

5. Constructor & Destructor

For Constructor – refer to SL solutions notes (Pg 16 & Page 70

For Destructor (

-------------------------------------------------------------------------------------------------------------------------

6. Inheritance

Refer to SL solution (Page 58

-----------------------------------------------------------------------------------------------------------------------------------

7. Polymorphism

Refer to SL solution (Page 59

---------------------------------------------------------------------------------------------------------------------------------

8. Derived and Base class

Derived class:

A derived class is a class created or derived from another existing class. The existing class from which the derived class is created through the process of inheritance is known as a base class or superclass. 

Derived classes are used for augmenting the functionality of base class by adding or modifying the properties and methods to suit the requirements of the specialization necessary for derived class. This allows for defining virtual methods that form the means to implement polymorphism, which allows a group of objects to work in uniform manner. Thus, the inherent advantages of inheritance and polymorphism like code reuse, faster development, easy maintenance, etc., are realized.

A derived class is also known as subclass or child class.

The hierarchical relationship between derived class and base class is known as an “is a” relationship. For example, consider a base class, LivingBeing, which is used to create two derived classes, Plant and Animal. Plant is a LivingBeing and Animal is a LivingBeing. Both have few common features but each type can have features that are unique to its specialization and are different from the features of the base class. 

While inheriting from base class, the derived class implicitly inherits all the members (except constructors and destructors), which it reuses as it extends and modifies the behavior of the base class. The derived class overrides the properties and methods of the base class so that it represents the specialized version of base class.

Based class:

A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors). A programmer can extend base class functionality by adding or overriding members relevant to the derived class. 

A base class may also be called parent class or superclass.

A class derived from a base class inherits both data and behavior. For example, "vehicle" can be a base class from which "car" and "bus" are derived. Cars and buses are both vehicles, but each represents its own specialization of the vehicle base class.

A base class has the following properties:

• Base classes are automatically instantiated before derived classes.

• The derived class can communicate to the base class during instantiation by calling the base class constructor with a matching parameter list.

• Base class members can be accessed from the derived class through an explicit cast.

• If abstract methods are defined in a base class, then this class is considered to be an abstract class and the non-abstract derived class should override these methods.

• Abstract base classes are created using the "abstract" keyword in its declaration and are used to prevent direct initiation using the "new" keyword.

---------------------------------------------------------------------------------------------------------------

9. Abstract Class, Pure virtual function

Abstract Class

A class with atleast one method that has no implementation is called as an abstract class. Other methods can be abstract or non abstract. By design, objects of abstract class cannot be created, but references can be created. So it is the responsibility of the developer to implement the abstract method in the derived class.

Abstract class is defined using abstract keyword. The abstract keyword enables creation of classes and class members solely for the purpose of inheritance – to define the common features of derived classes. Using abstract class, a hierarchy can be created that is easily extensible.

abstract class Shape{

public abstract void area();

}

public class Circle extends Shape{

int radius;

//.. other code

public void area(){

//code to find area of the circle

}

}

public class Rectangle extends Shape{

int length, bredth;

//.. other code;

public void area(){

//code for finding the area of rectangle

}

}

In the above example, the base class has one abstract method area() making it an abstract base class. The method has been implemented by its derived classes – Circle and Rectangle.

Following points should be noted regarding abstract classes:

• One cannot create objects of abstract classes. Any attempt to do so, results in compile time error.

• One can create references and therefore these classes support polymorphism.

• Abstract classes are useful when creating components because they allow specifying an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class in needed.

• Abstract methods dont have implementation

• A class inheriting from an abstract class must provide implementation to all abstract methods, else the class should be declared as abstract.

• Static modifiers cannot be used with abstract methods.

• Abstract modifiers cannot be used with constructors.

Pure virtual Function

A pure virtual function or pure virtualmethod is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

Abstract Class

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.

[pic]

Characteristics of Abstract Class

1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created.

2. Abstract class can have normal functions and variables along with a pure virtual function.

3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.

4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

[pic]

Pure Virtual Functions

Pure virtual Functions are virtual functions with no definition. They start with virtual keyword and ends with= 0. Here is the syntax for a pure virtual function,

virtual void f() = 0;

[pic]

Example of Abstract Class

class Base //Abstract base class

{

public:

virtual void show() = 0; //Pure Virtual Function

};

class Derived:public Base

{

public:

void show()

{ cout show();

}

Output : Implementation of Virtual Function in Derived class

In the above example Base class is abstract, with pure virtual show() function, hence we cannot create object of base class.

Pure Virtual definitions

• Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class.

• Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal.

class Base //Abstract base class

{

public:

virtual void show() = 0; //Pure Virtual Function

};

void Base :: show() //Pure Virtual definition

{

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download