Specific Features Of C Plus Plus Information Technology Essay

Published: November 30, 2015 Words: 1169

In c we use scanf function as standard input function,while in c++ we use streame cin>> for input.like this for output in c we use printf function,while in c++ we use cout<< as a output function.

In c we use #include<stdio.h>as iclusion file,while in c++ we use #include<iostreame>as inclusion file.

C is a topdown approach while c++ is bottom up approach

In c-programe the main function could not return a value but in the c++ the main function shuld return a value.

In C memory allocation is done with malloc statement whereasin C++ it is done through new keyword. Also memory is deallocated in C using free statement while in C++ deallocation takes place through delete.

Q2. Demonstrate the use of Inline Functions and Function Overloading.

ANS 2 :

When a function is declared inline, the function is expanded at the calling block. The function is not treated as a separate unit like other normal functions.

Inline functions are treated like macro definitions by the C++ compiler. They are declared with the keyword inline as follows.

//Declaration of inline :

int add(int x,int y);

//Definition of inline :

inline int add(int x,int y)

{

return x+y;

}

In fact, the keyword inline is not necessary. If the function is defined with its body directly and the function has a smaller block of code, it will be automatically treated as inline by the compiler.

A function is overloaded when same name is given to different function. However, the two functions with the same name will differ at least in one of the following.

a) The number of parameters

b) The data type of parameters

c) The order of appearance

#include<iostream.h>

#include<conio.h>

void display(int a)

{

cout<<a;

}

void display(char a)

{

cout<<a;

}

main()

{

display(2);

display('s');

getch();

return 0;

}

Q3.Discuss with the help of an example various parts of the Class Specification.

ANS 3 :

A class in C++ is an encapsulation of data members and functions that manipulate the data. The class can also have some other important members which are architecturally important.

Class Data Members:

The data members can be of any legal data type, a class type, a struct type etc.

Function members in classes:

Functions declared inside a class can be any of the four types.

Access Specifier:

Private: The members are accessible only by the member functions or friend functions.

Protected: These members are accessible by the member functions of the class and the classes which are derived from this class.

Public: Accessible by any external member.

Example of a class:

class Example_class

{

private:

int x; //Data member

int y; // Data member

public:

Example_Class()

{

x = 0;

y = 0;

}

int Add()

{

return x+y;

}

};

Q4. Justify the use of the various Access Specifiers, Is there any particular order in which they must be used.

ANS 4 :

Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language:

(a) Private

(b) Public

(c) Protected

A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class.

Public members are accessible from outside the class.

A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.

When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it.

class example

{

private:

int x,y;

public:

void sum()

{

………

………

}

};

There is no required order for access specifiers, and they may appear more than once. They affect all the members declared after them and before the next access specifier.

PART B

Q5. Give a comparison between Private Section and Protected Section in a Class with the help of an example.

ANS 5 :

Public members of a class A are accessible for all and everyone.

Protected members of a class A are not accessible outside of A's code, but is accessible from the code of any class derived from A.

Private members are only accessible within the class defining them.

Protected members are accessible in the class that defines them and in classes that inherit from that class.

Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.

class vehicle

{

protected:

char colorname[20];

int number_of_wheels;

public:

vehicle();

void start();

void stop();

void run();

};

class Car: public vehicle //derived class

{

protected:

char type_of_fuel;

public:

Car();

};

Q6. Create a class named Book .It should include the following:

Data Members:

Accession No.

Title of the Book

Author Name

Publisher

Member Functions:

To initialize the data members with appropriate data

To display details of the Book

ANS 6 :

#include<iostream.h>

#include<conio.h>

class book

{

int an;

char title[80];

char author[80];

char publisher[80];

public:

void getdata()

{

cout<<"Enter details of the Book :\n\n";

cout<<"\nAccession No.:";cin>>an;

cout<<"\nTitle of the Book:";cin>>title;

cout<<"\nAuthor Name:";cin>>author;

cout<<"\nPublisher:";cin>>publisher;

}

void display()

{

cout<<"\n\n\n\nDetails of the Book:";

cout<<"\n\nAccession No.:";cout<<an;

cout<<"\n\nTitle of the Book:";cout<<title;

cout<<"\n\nAuthor Name:";cout<<author;

cout<<"\n\nPublisher:";cout<<publisher;

}

};

main()

{

book b1;

b1.getdata();

b1.display();

getch();

return 0;

}

Q7. Discuss the various applications of Scope Resolution Operator in various Circumstances.

ANS :

It resolves global scope to local scope:

int count = 0;

int main(void) {

int count = 0;

::count = 1; // set global count to 1

count = 2; // set local count to 2

return 0;

}

To convey ownership of class towards a function:

class ABC{

public:

static void fun()

{

cout<<"ABC"<<endl;

}

};

class XYZ{

public:

static void fun()

{

cout<<"XYZ"<<endl;

}

};

int main()

{

ABC::fun();

XYZ::fun();

return 0;

}

To define a function outside the class when it is declared inside the class:

int a,b,c;

class a

{

public:

void sum();

};

a::sum()

{

c=a+b;

}

Q8. Give a Comparison between the Class and an Object, with the help of a small example.

ANS :

A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.

The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

#include<iostream.h>

class abc

{

char name[60],address[60];

public:

info()

{

cin>>name;

cin>>address;

}

display()

{

cout<<name;

cout<<address;

}

};

main()

{

abc s1,s2;

s1.info();

s1.display();

s2.info();

s2.display();

return 0;

}