darshan institute of engineering & technology for diploma ...€¦ · the complier replaces the...

13
Darshan Institute of Engineering & Technology for Diploma Studies Unit-2 1 Dept: CE Programming In C++ (3330702) Nitin Rola 1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value in another variable at function definition. In call by value the original value in calling function will never change after execution of function. For example: #include<iostream.h> void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } int main() { int a,b; cout<<"Enter two numbers:"; cin>>a>>b; swap(a, b); cout<<”a=”<<a<<”b=”<<b; return 0; } Call By Reference In call by reference pass reference when call function. The formal arguments in the called function become aliases to the actual argument in the calling function. In call by reference the original value in calling function will change after execution of function. For example: #include<iostream.h> void swap(int &a, int &b) { int temp; temp=a; a=b; b=temp; } int main() { int a,b; cout<<"Enter two numbers:"; cin>>a>>b; swap(a, b); cout<<”a=”<<a<<”b=”<<b; return 0; } 2. Explain return by reference A function can also return reference. Example:

Upload: others

Post on 07-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

1 Dept: CE Programming In C++ (3330702) Nitin Rola

1. Explain Call by Value vs. Call by Reference Or Write a program to interchange

(swap) value of two variables.

Call By Value

In call by value pass value, when we call the function.

And copy this value in another variable at function definition.

In call by value the original value in calling function will never change after execution of

function.

For example: #include<iostream.h>

void swap(int a, int b)

{

int temp;

temp=a;

a=b;

b=temp;

}

int main()

{

int a,b;

cout<<"Enter two numbers:";

cin>>a>>b;

swap(a, b);

cout<<”a=”<<a<<”b=”<<b;

return 0;

}

Call By Reference

In call by reference pass reference when call function.

The formal arguments in the called function become aliases to the actual argument in the

calling function.

In call by reference the original value in calling function will change after execution of

function.

For example: #include<iostream.h>

void swap(int &a, int &b)

{

int temp;

temp=a;

a=b;

b=temp;

}

int main()

{

int a,b;

cout<<"Enter two numbers:";

cin>>a>>b;

swap(a, b);

cout<<”a=”<<a<<”b=”<<b;

return 0;

}

2. Explain return by reference

A function can also return reference.

Example:

Page 2: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

2 Dept: CE Programming In C++ (3330702) Nitin Rola

int & max(int &x, int &y)

{

if(x > y)

return x;

else

return y;

}

Now max function will return reference of x or y.

3. What is inline function? Explain with example.

The functions can be made inline by adding prefix inline to the function definition.

An inline function is a function that is expanded in line when it is invoked.

The complier replaces the function call with the corresponding function code.

Inline function saves time of calling function, saving registers, pushing arguments onto the stack

and returning from function.

We should be careful while using inline function. If function has 1 or 2 lines of code and simple

expressions then only it should be used.

Inline expansion may not work in following situations,

1) If a loop, a switch or a goto exists in function body.

2) For function is not returning any value, if a return statement exists.

3) If function contains static variables.

4) If function is recursive.

Example: #include<iostream.h>

inline int cube(int n)

{

return n*n*n;

}

int main()

{

int c;

c = cube(10);

cout<<c;

return 0;

}

Function call is replaced with expression so c = cube(10); becomes c=10*10*10; at compile time.

Disadvantage:

It makes the program to take up more memory because the statements that define the inline

function are reproduced at each point where the function is called.

4. Default Arguments

C++ allows us to call a function without specifying all its arguments.

In such cases, the function assigns a default value to the parameter which does not have a matching

argument in the function call.

Default values are specified when the function is declared.

We must add default arguments from right to left.

We cannot provide a default value to a particular argument in the middle of an argument list.

Page 3: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

3 Dept: CE Programming In C++ (3330702) Nitin Rola

Default arguments are useful in situations where some arguments always have the same value. E.g.

passing marks.

Legal and illegal default arguments

void f(int a, int b, int c=0); // legal

void f(int a, int b=0, int c=0); // legal

void f(int a=0, int b, int c=0); // illegal

void f(int a=0, int b, int c); // illegal

void f(int a=0, int b=0, int c=0); // legal

Example: #include <iostream.h>

void f(int a=0, int b=0)

{

cout << "a= " << a << ", b= " << b;

cout << '\n';

}

int main()

{

f();

f(10);

f(10, 99);

return 0;

}

Output: a=0,b=0

a=10,b=0

a=10, b=99

5. Explain function overloading with example.

Function overloading is compile time polymorphism.

Function overloading is the practice of declaring the same function with different signatures.

The same function name will be used with different number of parameters and parameters of

different type.

Overloading of functions with different return types is not allowed.

Compiler identifies which function should be called out of many using the type and number of

arguments.

A function is overloaded when same name is given to different functions. However, the two functions

with the same name must differ in at least one of the following,

a) The number of parameters

b) The data type of parameters

c) The order of appearance

Example: #include <iostream.h>

void Add(int num1, int num2)\\ Function 1: Receives 2 integer parameters

{

cout<<num1 + num2 <<endl;

}

void Add(float num1, float num2)\\Function 2: Receives 2 float parameters.

{

cout<<num1 + num2 <<endl;

Page 4: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

4 Dept: CE Programming In C++ (3330702) Nitin Rola

}

void Add(int num1, int num2, int num3)\\Function 3: Receives 3 int parameters

{

cout<<num1 + num2 + num3 <<endl;

}

int main()

{

float a=10.5,b=20.5;

Add(10,20); \\ Calls function 1

Add(a,b); \\ Calls function 2

Add(1,2,3); \\ Calls function 3

return 0;

}

6. Explain Const argument

Function should not modify const argument.

Compiler will generate error when condition is violated.

Example: int length(const string &s); int trial(const int a=10);

7. Class v/s Structure

Class Structure

1. By default members of class are private. 1. By default members of structure are public.

2.Example: class student

{

int rollno;

public:

void getdata();

};

2.Example: struct student

{

int rollno;

void getdata();

};

8. Explain Class with example

A class is a template that specifies the attributes and behavior of things or objects.

A class is a blueprint or prototype from which objects are created.

A class is the implementation of an abstract data type (ADT). It defines attributes and

methods which implement the data structure and operations of the ADT, respectively.

The General Form of a Class class classname {

Datatype variable1;

Datatype variable2;

// ...

Datatype variableN;

Public:

Returntype methodname1(parameter-list){

// body of method }

Returntype methodname2(parameter-list) {

// body of methodANGUAGE}

Returntype methodnameN(parameter-list) {

// body of method }

}

Page 5: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

5 Dept: CE Programming In C++ (3330702) Nitin Rola

A class is declared by use of the class keyword.

The data, or variables, defined within a class are called instance variables. The code is

contained within methods. Collectively, the methods and variables defined within a class are

called members of the class.

Variables defined within a class are called instance variables because each instance of the

class (that is, each object of the class) contains its own copy of these variables. Thus, the data

for one object is separate and unique from the data for another.

Example class Box

{

double width;

double height;

double depth;

public:

void volume()

{

cout<<"Volume is ";

cout<<(width * height * depth)<<endl;

}

}

9. Creating Objects

We can create by using class name.

Syntax: class_name Object_name;

Example: Box b

Here b is a object of class Box and Box is datatype of object b;

10. Introducing Methods

General form of a method:

type name(parameter-list) {

// body of method }

Here, type specifies the type of data returned by the method. This can be any valid type,

including class types that you create. If the method does not return a value, its return type

must be void.

The name of the method is specified by name. This can be any legal identifier other than

those already used by other items within the current scope. The parameter-list is a sequence

of type and identifier pairs separated by commas.

Parameters are essentially variables that receive the value of the arguments passed to the

method when it is called. If the method has no parameters, then the parameter list will be

empty.

Return value

Methods that have a return type other than void return a value to the calling routine using the

following form of the return statement: return value;

Here, value is the value returned.

Example class Box

{

double width;

double height;

double depth;

double volume(){

Page 6: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

6 Dept: CE Programming In C++ (3330702) Nitin Rola

return width * height * depth; }

}

11. Explain Memory allocation for objects

The memory space for objects is allocated when they are declared and not when the class is

specified. This is partly true.

The member function are created and placed in the memory space only once when they are

defined as a part of a class specification.

All the objects belonging to that class use the same member functions; no separate space is

allotted when objects are created.

Only space for member variables is allocated separately for each object

Common for all objects

Member funcion1

Memory created when functions

defined

Member funcion2

Object1 Object2 Object3

Member variable1 Member variable1 Member variable1

Member variable2 Member variable2 Member variable2

Memory created when objects

defined

12. What is friend function? Explain with example.

A friend function is a function which is declared using friend keyword.

It is not a member of the class but it has access to the private and protected members of the class.

It is not in the scope of the class to which it has been declared as friend.

It cannot access the member names directly.

It can be declared either in public or private part of the class.

It is not a member of the class so it cannot be called using the object.

Usually, it has the objects as arguments.

It is normal external function which is given special access privileges.

Syntax:

class ABC

{

public:

……………………………………………

friend void xyz(void); // declaration

……………………………………………

};

Page 7: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

7 Dept: CE Programming In C++ (3330702) Nitin Rola

void xyz(void) //definition

{

Set of statements;

}

xyz() //call

Example: #include<iostream.h>

#include<conio.h>

class numbers

{

int num1, num2;

public:

void setdata(int a, int b);

friend int add(numbers N);

};

void numbers :: setdata(int a, int b)

{

num1=a;

num2=b;

}

int add(numbers N)

{

return (N.num1+N.num2);

}

int main()

{

numbers N1;

N1.setdata(10,20);

cout<<”Sum = ”<<add(N1);

getch();

return 0;

}

add is a friend function of the class numbers so it can access all members of the class (private,

public and protected).

Member functions of one class can be made friend function of another class, like…

class X

{

………………………………………

int f();

}

class Y

{

………………………………………

friend int X :: f();

}

The function f is a member of class X and a friend of class Y.

We can declare all the member functions of one class as the friend functions of another class. In

such cases, the class is called a friend class, like class X is the friend class of class Z

class Z

{

Page 8: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

8 Dept: CE Programming In C++ (3330702) Nitin Rola

………………………………………

friend class X;

……………………………………….

}

13. Explain various type of Access modifier (specifier) in C++. OR

Explain various scope of class.

C++ has three access modifiers namely private, protected and public.

Private:

Private members of the class can be accessed within the class and from member functions of the

class.

They cannot be accessed outside the class or from other programs, not even from inherited class.

Encapsulation is possible due to the private access modifier.

If one tries to access the private members outside the class then it results in a compile time error.

Protected:

This access modifier plays a key role in inheritance.

Protected members of the class can be accessed within the class and from derived class but cannot

be accessed from any other class or program.

It works like public for derived class and private for other programs

Public:

Public members of the class are accessible by any program from anywhere.

There are no restrictions for accessing public members of a class.

Class members that allow manipulating or accessing the class data are made public.

Example: class ABC

{

int A; // private variable by default

int GetA() { return A; } // private method by default

private:

int B; // private variable

int GetB() { return B; } // private method

protected:

int C; // protected variable

int GetC() { return C; } // protected method

public:

int D; // public variable

int GetD() { return D; } // public method

};

class XYZ : public ABC

{

//A, B, GetA(),GetB() cannot be accessed from here because they are private

//C, GetC() can be accessed from here because they are protected

//D, GetD() can be accessed from here and anywhere because they are public

};

Page 9: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

9 Dept: CE Programming In C++ (3330702) Nitin Rola

int main()

{

ABC a;

a.D = 5; // can be accessed because D is public

cout << a.GetD(); // can be accessed because GetD() is public

a.A = 2; // cannot be accessed because A is private

cout << a.GetB(); // cannot be accessed because GetB() is private

a.C = 2; // cannot be accessed because C is protected

getch();

return 0;

}

14. Explain Static data members and static member functions with example.

Static data members

Data members of the class which are shared by all objects are known as static data members.

Only one copy of a static variable is maintained by the class and it is common for all objects.

Static members are declared inside the class and defined outside the class.

It is initialized to zero when the first object of its class is created. No other initialization is

permitted.

It is visible only within the class but its lifetime is the entire program.

Static members are generally used to maintain values common to the entire class.

Example: #include<iostream.h>

#include<conio.h>

class item

{

int number;

static int count; \\ static variable declaration

public:

void getdata(int a)

{

number = a;

count++;

}

void getcount()

{

cout<<”Count : “<<count;

}

};

int item :: count; \\ static variable definition

int main()

{

item a,b,c;

a.getdata(100);

b.getdata(200);

c.getdata(300);

a.getcount();

b.getcount();

c.getcount();

getch();

return 0;

}

Object a, b and c have separate storage area for variable number but they share common storage

area for count variable

Static member functions

Static member functions are associated with a class, not with any object.

Object c Object b Object a

300 200 100

3

count

Page 10: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

10 Dept: CE Programming In C++ (3330702) Nitin Rola

They can be invoked using class name, not object.

They can access only static members of the class.

They cannot be virtual.

They cannot be declared as constant or volatile.

A static member function can be called, even when a class is not instantiated.

There cannot be static and non-static version of the same function.

A static member function does not have this pointer.

Syntax:

classname:: functionname

Example:

#include<iostream.h> #include<conio.h>

class item

{

int number;

static int count;

public:

void getdata(int a)

{

number = a;

count++;

}

static void getcount()

{

cout<<”Count : “<<count;

}

};

int item :: count;

int main()

{

item a,b,c;

a.getdata(100);

b.getdata(200);

c.getdata(300);

item::getcount();

getch();

return 0;

}

15. Explain nesting of member functions.

A member function can be called by using its name inside another function of the same class is

known as a nesting of member function

For example: class sample

{

int a;

pulibc :

void getdata()

{

cout<<”Enter the value of a”;

cin>>a;

}

void putdata()

{

getdata() //nesting of member function

cout<<”the value of a=”<<a;

}

Page 11: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

11 Dept: CE Programming In C++ (3330702) Nitin Rola

};

int main()

{

sample s;

s.putdata();

getch();

return 0;

}

When we call putdata() function, the control will jump to the definition of putdata function and

from this definition control will jump to the definition of getdata() function when detect call

statement of getdata().

16. Differentiate public member function and private member function by proper

example.

We cannot call private member function using object, where as we can public member function.

A private member function can only be called by another member function of same class.

To call private member function, we have to use nesting of member function.

For example: #include <iostream.h>

#include <conio.h>

class sample

{

int a;

void read() //private member function

{

cout<<"Enter the value of a";

cin>>a;

}

public:

void disp() //public member function

{

read(); //call private member function

cout<<"The value of a="<<a;

}

};

int main()

{

sample S;

S.disp(); //call public member function

getch();

return 0;

}

/* OUTPUT Enter the value of a10

The value of a=10 */

17. Write a program to demonstrate the array of object.

we can also create array of object like as array of any type of variable. #include <iostream.h>

class sample

{

int roll_no;

char name[20];

public:

void read()

Page 12: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

12 Dept: CE Programming In C++ (3330702) Nitin Rola

{

cout<<"Enter the Roll number=";

cin>>roll_no;

cout<<"Enter the Name=";

cin>>name;

}

void disp()

{

cout<<"\nRoll_no="<<roll_no<<"\tName="<<name;

}

};

void main()

{

sample S[5];

int i;

for(i=0;i<5;i++)

S[i].read();

for(i=0;i<5;i++)

S[i].disp();

}

18. Making an outside function inline

Define member function outside the class to separate the detail of implementation from the class.

We can make outside function inline by following method.

Example: class trial

{

int number;

public:

void getdata(int a);

};

inline void trial:: getdata(int a)

{

number=a;

}

19 Explain array within a class

We can also declare and use array within class like as outside class.

Example: #include <iostream.h>

#include <conio.h>

class trial

{

int a[5];

public:

void getdata()

{

for(int i=0;i<5;i++)

{

cin>>a[i];

}

}

void putdata()

{

for(int i=0;i<5;i++)

{

cout<<a[i];

}

}

};

Page 13: Darshan Institute of Engineering & Technology for Diploma ...€¦ · The complier replaces the function call with the corresponding function code. Inline function saves time of calling

Darshan Institute of Engineering & Technology for Diploma Studies Unit-2

13 Dept: CE Programming In C++ (3330702) Nitin Rola

void main()

{

trial t;

clrscr();

t.getdata();

t.putdata();

getch();

}

20 Explain passing objects as an argument and return object

We can pass object as an argument like any other data type and also we can return it.

We can pass by two methods:

1. A copy of the entire object is passed to the function.

2. Only the address of the object is transferred to the function.

We can return object by return keyword and set return type of function is class_name.

Example: #include <iostream.h>

#include <conio.h>

class trial

{

int a;

public:

void getdata()

{

a=10;

}

void putdata()

{

cout<<"\nValue of a="<<a;

}

trial square(trial tt)

{

trial ttt;

a=tt.a*tt.a;

ttt.a=tt.a*tt.a*tt.a;

return ttt;

}

};

void main()

{

trial t1,t2,t3;

clrscr();

t1.getdata();

t3=t2.square(t1);

t1.putdata();

t2.putdata();

t3.putdata();

getch();

}

Output Value of a=10

Value of a=100

Value of a=1000