Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of UK Essays.
Ans1. Unique features of C++ :
Encapsulation: It is the process of combining data and functions into a single unit called class. Using the method of encapsulation, the programmer cannot directly access the data rather data is accessible through the functions present inside the class. It led to the important concept of data hiding.
Abstraction: It is one of the most powerful and vital features provided by object-oriented C++ programming language. The main idea behind data abstraction is to give a clear separation between properties of datatype and the associated implementation details.
Polymorphism: It is the ability to use an operator or function in different ways. Poly, referring to many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism.
Inheritance: It is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived classes.
C++ better than C:
Stronger typing: the type system in C++ is stronger than in C. This prevents many common programming errors – coupled with the next very important feature, the stronger type system even manages not be an inconvenience.
A Bigger standard library: C++ allows the full use of the standard library. It includes the Standard Template Library.
Parameterized types: the template keyword allows the programmer to write generic implementations of algorithms.
Data and methods to edit the data act as one entity i.e. by the usage of classes.
Limiting scope of data i.e. by using private/public variables.
Constructors and destructors for defining default behaviour of entities.
Ques2. Demonstrate the use of Inline Functions and Function Overloading.
Ans2. An Inline function is a function that is expanded in line when it is invoked. This kind of function is used to save the memory space which becomes appreciable when a function is likely to be called a number of times. It is used to eliminate the cost of calls to small functions. Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call but in inline function, when the program is compiled, the code present in the function body is replaced in place of the function call.
inline datatype function_name(arguments)
inline float mul(float x, float y)
inline double div(double p, double q)
float a= 12.345;
float b= 9.82;
Hence, inline expansion makes a program run faster because the overhead of the function call and return is eliminated.
In C++, two or more functions can share name as long as their parameter declarations are different. In this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading. It is one of the way in C++ to achieve Polymorphism.
In general, to overload a function, simply we can declare different versions of it but there comes an important restriction: the type and/or number and/or sequence of the parameters of each overloaded function must differ.
//Overload a function three times.
void f(int i);
void f(int i, int j);
void f(double k);
void f(int i)
cout<<“In f(int), i is “<<i<<‘n’;
void f(int i, int j)
cout<<“In f(int, int), i is “<<i;
cout<<“,j is “<<‘n’;
void f(double k)
cout<<“In f(double), k is ” <<k<<‘n’;
As illustrated above, f() is overloaded three times. The first version takes one integer parameter, the second version requires two integer parameters, and the third version has one double parameter. Because the parameter list for each version is different, the compiler is able to call the correct version of each function based on the type of the arguments specified at the time of the call.
Ques3. Discuss with the help of an example various parts of the Class Specification.
Ans3. A class is a way to bind the data and its associated functions together. It allows the data to be hidden, if necessary, from external use. When defining a class, we are creating a new abstract data type that can be treated like any other built-in data type. Generally, a class specification has two parts:
Class Function Definitions
The class declaration describes the type and scope of its members. The keyword class specifies, that what follows is an abstract data of the type class_name. The body of the class is enclosed within braces and terminated by a semicolon. The class body contains the declaration of variables and functions which are collectively called class members. They are usually grouped under three sections, namely, private, public and protected. The keywords private, public and protected are known as visibility labels.
Class Function Definition:
The class function definitions describe how the class functions are implemented. The definition of the member functions of the class can be done either inside or outside the class. The body of the member function is analysed after the class declaration so that members of that class can be used in the member function body, even if the member function definition appears before the declaration of that member in the class member list. The member functions of the class are usually declared as public i.e. they can be accessed outside the class. We can declare a member function as static, known as static member function or it can not be declared as static, known as nonstatic member function.
The general form of a class declaration is:
void getdata(int a, float b);
Ques4. Justify the use of the various Access Specifiers, Is there any particular order in
which they must be used.
Ans4. In a class specifier, the members whether data or functions can be private, meaning they can be accessed only by member functions of that class, or public, meaning they can be accessed by any function in the program. The primary mechanism for hiding the data is to put it in a class as private. Data hidng with the security techniques is used to protect computer databases. The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed from outside the class.
There are three access specifiers given by C++:
Private: which disallows the accessibility of block outside the class.
Public: which allows the accessibility to any function outside the class.
Protected: which resists the accessibility upto derived class only.
With the proper use of access specifiers, the data can be hidden from unauthorised access.
In procedural languages, variables (global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As the data members and also the member functions of a class cannot be accessed outside the class if they have private access so they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from base class then the derived class cannot access the private members of the base class. In addition if a class is derived from another class privately i.e. for example syntax: class B : private A, is used then all the public and the protected members becomes private to class B and cannot be accessed outside the class B, even by using the object of class B.
There is no specific order required for member access. The allocation of storage for objects of class types is implementation dependent, but members are guaranteed to be assigned successively higher memory addresses between access specifiers.
Ques5. Give a comparison between Private Section and Protected Section in a Class with the help of an example.
Ans5. The comparison between Private Section and Protected Section is stated below:
Private member can only be accessed in a class but access is denied from the derived class but protected can be accessed by the derived class.
Private makes a member visible within a class since protected makes it visible for the class and all derived classes.
Ques6. Create a class named Book .It should include the following:
Title of the Book
To initialize the data members with appropriate data
To display details of the Book
char title, author,publisher;
cout<<“nEnter the accession Number of the book: “;
cout<<“nEnter the title of the book: “;
cout<<“nEnter the name of the author: “;
cout<<“nEnter the name of the Publisher:”;
cout<<“nThe information about the book: nnAccession Number: “<<acc_no<<“ttTitle: “<<title<<“nAuthor Name: “<<author<<“ttPublisher: “<<publisher;
Ques7. Discuss the various applications of Scope Resolution Operator in various
Ans7. Applications of Scope Resolution Operators:
Defining the Function Members outside the class: the member functions of a particular class, i.e. the functions declared inside particular class can be defined outside the class and thus the private members of the class can be accessed.
Defining the Function Members inside the main() function: the member functions of a particular class, i.e. the functions declared inside particular class can also be defined inside the main() function and thus the private members of the class can be accessed.
Using Friend Function: the private members of the class can also be accessed through the friend functions.
Ques8. Give a Comparison between the Class and an Object, with the help of a small
Class is an entity used for wrapping of data and functions whereas object is an individual instance of a class.
Class refers to a definition and object refers to the values that we put in the definition.
The members of the class are not allocated the memory space when class is created instead the memory space is allocated when the object is created.
In the statement : int a; —int refers to the data type and a refers to the variable name similarly in the statement: cat frisky; (refer to the example below) —cat refers to the data type and frisky refers to the variable name
The members of the class can be accessed only through the object not with the class.
One class can have many objects.
Class is defined outside the main() function whereas object can be defined along with the class definition or inside the main() function.
This code defines frisky, which is an object whose class (or type) is cat. C++ differentiates between the class Cat, which is the idea of a cat, and each individual Cat object. Thus, frisky is an object of type cat in the same way in which itsage is a variable of type int.
If you need assistance with writing your essay, our professional essay writing service is here to help!Find out more
Cite This Work
To export a reference to this article please select a referencing style below:
Related ServicesView all
DMCA / Removal Request
If you are the original writer of this essay and no longer wish to have the essay published on the UK Essays website then please: