The unique features of C++ in programming

Ans1. Unique characteristics of C++ :

Encapsulation: It is the procedure of uniting informations and maps into a individual unit called category. Using the method of encapsulation, the coder can non straight entree the information instead information is accessible through the maps present inside the category. It led to the of import construct of informations concealment.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

Abstraction: It is one of the most powerful and critical characteristics provided by object-oriented C++ scheduling linguistic communication. The chief thought behind informations abstraction is to give a clear separation between belongingss of datatype and the associated execution inside informations.

Polymorphism: It is the ability to utilize an operator or map in different ways. Poly, mentioning to many utilizations of these operators and maps. A individual map use or an operator working in many ways can be called polymorphism.

Inheritance: It is the procedure by which new categories called derived categories are created from bing categories called base categories. The derived categories have all the characteristics of the base category and the coder can take to add new characteristics specific to the freshly created derived categories.

C++ better than Degree centigrades:

Stronger typewriting: the type system in C++ is stronger than in C. This prevents many common programming mistakes – coupled with the following really of import characteristic, the stronger type system even manages non be an incommodiousness.

A Bigger standard library: C++ allows the full usage of the standard library. It includes the Standard Template Library.

Parameterized types: the templet keyword allows the coder to compose generic executions of algorithms.

Data and methods to redact the informations act as one entity i.e. by the use of categories.

Restricting range of informations i.e. by utilizing private/public variables.

Builders and destructors for specifying default behavior of entities.

Ques2. Demonstrate the usage of Inline Functions and Function Overloading.

Ans2. An Inline map is a map that is expanded in line when it is invoked. This sort of map is used to salvage the memory infinite which becomes appreciable when a map is likely to be called a figure of times. It is used to extinguish the cost of calls to little maps. Normally, a map call transfers the control from the naming plan to the map and after the executing of the plan returns the control back to the naming plan after the map call but in inline map, when the plan is compiled, the codification nowadays in the map organic structure is replaced in topographic point of the map call.

Syntax:

inline datatype function_name ( statements )

{

map organic structure

}

Examples:

# include & lt ; iostream, H & gt ;

# include & lt ; conio.h & gt ;

inline float mul ( float ten, float Y )

{

return ( x*y ) ;

}

inline dual div ( dual P, dual Q )

{

return ( p/q ) ;

}

int chief ( )

{

clrscr ( ) ;

float a= 12.345 ;

float b= 9.82 ;

cout & lt ; & lt ; mul ( a, B ) & lt ; & lt ; ”
” ;

cout & lt ; & lt ; div ( a, B ) & lt ; & lt ; ”
” ;

return 0 ;

}

Hence, inline enlargement makes a plan run faster because the operating expense of the map call and return is eliminated.

Function Overloading:

In C++ , two or more maps can portion call every bit long as their parametric quantity declarations are different. In this state of affairs, the maps that portion the same name are said to be overloaded, and the procedure is referred to as map overloading. It is one of the manner in C++ to accomplish Polymorphism.

In general, to overload a map, merely we can declare different versions of it but there comes an of import limitation: the type and/or figure and/or sequence of the parametric quantities of each overladen map must differ.

Examples:

//Overload a map three times.

# include & lt ; iostream & gt ;

# include & lt ; conio.h & gt ;

nothingness degree Fahrenheit ( int I ) ;

nothingness degree Fahrenheit ( int I, int J ) ;

nothingness degree Fahrenheit ( dual K ) ;

int chief ( )

{

clrscr ( ) ;

degree Fahrenheit ( 10 ) ;

degree Fahrenheit ( 10, 20 ) ;

degree Fahrenheit ( 12.23 ) ;

getch ( ) ;

return 0 ;

}

nothingness degree Fahrenheit ( int I )

{

cout & lt ; & lt ; ” In degree Fahrenheit ( int ) , I is “ & lt ; & lt ; I & lt ; & lt ; ‘
‘ ;

}

nothingness degree Fahrenheit ( int I, int J )

{

cout & lt ; & lt ; ” In degree Fahrenheit ( int, int ) , I is “ & lt ; & lt ; I ;

cout & lt ; & lt ; ” , J is “ & lt ; & lt ; ‘
‘ ;

}

nothingness degree Fahrenheit ( dual K )

{

cout & lt ; & lt ; ” In degree Fahrenheit ( dual ) , k is “ & lt ; & lt ; k & lt ; & lt ; ‘
‘ ;

}

As illustrated above, f ( ) is overloaded three times. The first version takes one whole number parametric quantity, the 2nd version requires two whole number parametric quantities, and the 3rd version has one dual parametric quantity. Because the parametric quantity list for each version is different, the compiler is able to name the right version of each map based on the type of the statements specified at the clip of the call.

Ques3. Discuss with the aid of an illustration assorted parts of the Class Specification.

Ans3. A category is a manner to adhere the informations and its associated maps together. It allows the informations to be hidden, if necessary, from external usage. When specifying a category, we are making a new abstract informations type that can be treated like any other constitutional informations type. Generally, a category specification has two parts:

Class Declaration

Class Function Definitions

Class Declaration:

The category declaration describes the type and range of its members. The keyword category specifies, that what follows is an abstract information of the type class_name. The organic structure of the category is enclosed within braces and terminated by a semicolon. The category organic structure contains the declaration of variables and maps which are jointly called category members. They are normally grouped under three subdivisions, viz. , private, public and protected. The keywords private, public and protected are known as visibleness labels.

Class Function Definition:

The category map definitions depict how the category maps are implemented. The definition of the member maps of the category can be done either inside or outside the category. The organic structure of the member map is analysed after the category declaration so that members of that category can be used in the member map organic structure, even if the member map definition appears before the declaration of that member in the category member list. The member maps of the category are normally declared as public i.e. they can be accessed outside the category. We can declare a member map as inactive, known as inactive member map or it can non be declared as inactive, known as nonstatic member map.

The general signifier of a category declaration is:

category class_name

{

private:

variable declarations ;

map declarations ;

populace:

variable declarations ;

map declarations ;

} ;

Examples:

category point

{

int figure ;

float cost ;

populace:

nothingness getdata ( int a, float B ) ;

nothingness putdata ( null ) ;

} ;

Ques4. Justify the usage of the assorted Access Specifiers, Is at that place any peculiar order in

which they must be used.

Ans4. In a category specifier, the members whether informations or maps can be private, intending they can be accessed merely by member maps of that category, or public, intending they can be accessed by any map in the plan. The primary mechanism for concealing the information is to set it in a category as private. Data hidng with the security techniques is used to protect computing machine databases. The information is hidden so it will be safe from inadvertent use, while the maps that operate on the informations are public so they can be accessed from outside the category.

There are three entree specifiers given by C++ :

Private: which disallows the handiness of block outside the category.

Public: which allows the handiness to any map outside the category.

Protected: which resists the handiness upto derived category merely.

With the proper usage of entree specifiers, the informations can be hidden from unauthorized entree.

Justification:

In procedural linguistic communications, variables ( planetary variables ) are free to flux from maps to maps and hence they were non secured. But in C++ , merely the category in which the information members are being declared can entree them by utilizing private specifier. As the information members and besides the member maps of a category can non be accessed outside the category if they have private entree so they get hidden from the user and therefore the informations concealment is achieved. Besides in heritage when we derive a category from base category so the derived category can non entree the private members of the base category. In add-on if a category is derived from another category in private i.e. for illustration sentence structure: category B: private A, is used so all the populace and the protected members becomes private to category B and can non be accessed outside the category B, even by utilizing the object of category B.

There is no specific order required for member entree. The allotment of storage for objects of category types is implementation dependant, but members are guaranteed to be assigned in turn higher memory references between entree specifiers.

Part B

Ques5. Give a comparing between Private Section and Protected Section in a Class with the aid of an illustration.

Ans5. The comparing between Private Section and Protected Section is stated below:

Private member can merely be accessed in a category but entree is denied from the derived category but protected can be accessed by the derived category.

Private makes a member seeable within a category since protected makes it seeable for the category and all derived categories.

Ques6. Make a category named Book.It should include the undermentioned:

Datas Members:

Accession No.

Title of the Book

Writer Name

Publisher

Member Functions:

To initialise the informations members with appropriate informations

To expose inside informations of the Book

Ans6. # include & lt ; iostream.h & gt ;

# include & lt ; conio.h & gt ;

category book

{

int acc_no ;

char rubric [ 20 ] , writer [ 20 ] , publishing house [ 20 ] ;

populace:

nothingness getdata ( )

{

cout & lt ; & lt ; ”
Enter the accession Number of the book: “ ;

cin & gt ; & gt ; acc_no ;

cout & lt ; & lt ; ”
Enter the rubric of the book: “ ;

cin & gt ; & gt ; rubric ;

cout & lt ; & lt ; ”
Enter the name of the writer: “ ;

cin & gt ; & gt ; writer ;

cout & lt ; & lt ; ”
Enter the name of the Publisher: ” ;

cin & gt ; & gt ; publishing house ;

}

nothingness show ( )

{

cout & lt ; & lt ; ”
The information about the book:

Accession Number: “ & lt ; & lt ; acc_no & lt ; & lt ; ” Title: “ & lt ; & lt ; title & lt ; & lt ; ”
Author Name: “ & lt ; & lt ; writer & lt ; & lt ; ” Publisher: “ & lt ; & lt ; publishing house ;

}

} ;

int chief ( )

{

clrscr ( ) ;

book b1 ;

b1.getdata ( ) ;

b1.display ( ) ;

getch ( ) ;

return 0 ;

}

Ques7. Discuss the assorted applications of Scope Resolution Operator in assorted

Fortunes.

Ans7. Applications of Scope Resolution Operators:

Specifying the Function Members outside the category: the member maps of a peculiar category, i.e. the maps declared inside peculiar category can be defined outside the category and therefore the private members of the category can be accessed.

Specifying the Function Members inside the chief ( ) map: the member maps of a peculiar category, i.e. the maps declared inside peculiar category can besides be defined inside the chief ( ) map and therefore the private members of the category can be accessed.

Using Friend Function: the private members of the category can besides be accessed through the friend maps.

Ques8. Give a Comparison between the Class and an Object, with the aid of a little

illustration.

Ans8.

Class is an entity used for wrapping of informations and maps whereas object is an single case of a category.

Class refers to a definition and object refers to the values that we put in the definition.

The members of the category are non allocated the memory infinite when category is created alternatively the memory infinite is allocated when the object is created.

In the statement: int a ; — -int refers to the informations type and a refers to the variable name likewise in the statement: cat frisky ; ( mention to the illustration below ) — -cat refers to the informations type and kittenish refers to the variable name

The members of the category can be accessed merely through the object non with the category.

One category can hold many objects.

Class is defined outside the chief ( ) map whereas object can be defined along with the category definition or inside the chief ( ) map.

Examples:

category cat

{

int itsage ;

int itsweight ;

populace:

mew ( ) ;

} frisky ;

This codification defines frisky, which is an object whose category ( or type ) is cat. C++ differentiates between the category Cat, which is the thought of a cat, and each single Cat object. Therefore, frisky is an object of type cat in the same manner in which itsage is a variable of type int.

Leave a Reply

Your email address will not be published. Required fields are marked *