23
1 Introduction to C++ UshaRani.M Department of Computer Applications  Agni College of Technology Anna University, Chennai  

Call by Reference

Embed Size (px)

Citation preview

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 1/23

1

Introduction to C++

UshaRani.M

Department of Computer Applications

Agni College of TechnologyAnna University, Chennai

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 2/23

2

Contents

1 . Introduction 2 . C++ Single-Line Comments

3 . C++ Stream Input/Output 4 . Declarations in C++ 5 . Creating New Data Types in C++

6 . Reference Parameters 7 . Const Qualifier 8 . Default Arguments

9 . Function Overloading

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 3/23

3

1 . Introduction

C++ improves on many of C‟s features.

C++ provides object-oriented programming

(OOP). C++ is a superset to C.

No ANSI standard exists yet (in 1994).

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 4/23

4

2 . C++ Single-Line Comments

In C,

/* This is a single-line comment. */

In C++, // This is a single-line comment.

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 5/23

5

3 . C++ Stream Input/Output

In C,

printf(“Enter new tag: “);

scanf(“%d”, &tag); printf(“The new tag is: %d \ n”, tag);

In C++,

cout << “Enter new tag: “; cin >> tag; cout << “The new tag is : “ << tag << „ \ n‟;

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 6/23

6

3 .1 An Example

// Simple stream input/output

#include <iostream.h>

main() {

cout << "Enter your age: ";

int myAge;

cin >> myAge;

cout << "Enter your friend's age: ";

int friendsAge;

cin >> friendsAge;

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 7/23

7

if (myAge > friendsAge)

cout << "You are older.\n";

else

if (myAge < friendsAge) cout << "You are younger.\n";

else

cout << "You and your friend are thesame age.\n";

return 0;

}

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 8/23

8

4 . Declarations in C++

In C++, declarations can be placed anywhere(except in the condition of a while , do/while ,

for or if structure.) An example

cout << “Enter two integers: “;

int x, y;

cin >> x >> y; cout << “The sum of “ << x << “ and “ << y

<< “ is “ << x + y << ‘ \ n’;

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 9/23

9

Another example

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

cout << i << ‘ \ n’;

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 10/23

10

5 . Creating New Data Types in C++

struct Name {

char first[10];

char last[10];

};

In C, struct Name stdname;

In C++, Name stdname;

The same is true for enum s and union s

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 11/23

11

6 . Reference Parameters

In C, all function calls are call by value. – Call be reference is simulated using pointers

Reference parameters allows function argumentsto be changed without using return or pointers.

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 12/23

12

6 .1 Comparing Call by Value, Call by Referencewith Pointers and Call by Reference with References

#include <iostream.h>

int sqrByValue(int); void sqrByPointer(int *);

void sqrByRef(int &);

main() {

int x = 2, y = 3, z = 4;

cout << "x = " << x << " before sqrByVal\n" << "Value returned by sqrByVal: " << sqrByVal(x) << "\nx = " << x << " after sqrByVal\n\n";

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 13/23

13

cout << "y = " << y << " before sqrByPointer\n"; sqrByPointer(&y); cout << "y = " << y << " after sqrByPointer\n\n";

cout << "z = " << z << " before sqrByRef\n"; sqrByRef(z); cout << "z = " << z << " after sqrByRef\n";

return 0; }

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 14/23

14

int sqrByValue(int a) {

return a *= a;// caller's argument not modified

}

void sqrByPointer(int *bPtr) {

*bPtr *= *bPtr;// caller's argument modified

}

void sqrByRef(int &cRef)

{ cRef *= cRef;// caller's argument modified

}

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 15/23

15

Output

$ g++ -Wall -o square square.cc

$ square

x = 2 before sqrByValue Value returned by sqrByValue: 4

x = 2 after sqrByValue

y = 3 before sqrByPointer

y = 9 after sqrByPointer

z = 4 before sqrByRef

z = 16 after sqrByRef

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 16/23

16

7 . The Const Qualifier

Used to declare “ constant variables ” (instead of #define) const float PI = 3.14156;

The const variables must be initialized whendeclared.

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 17/23

17

8 . Default Arguments

When a default argument is omitted in a functioncall, the default value of that argument isautomatically passed in the call.

Default arguments must be the rightmost (trailing)arguments.

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 18/23

18

8 .1 An Example

// Using default arguments

#include <iostream.h>

// Calculate the volume of a boxint boxVolume(int length = 1, int width = 1,

int height = 1)

{ return length * width * height; }

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 19/23

19

main()

{

cout << "The default box volume is: "

<< boxVolume()

<< "\n\nThe volume of a box with length 10,\n"

<< "width 1 and height 1 is: "

<< boxVolume(10)

<< "\n\nThe volume of a box with length 10,\n"

<< "width 5 and height 1 is: "

<< boxVolume(10, 5)

<< "\n\nThe volume of a box with length 10,\n"

<< "width 5 and height 2 is: "

<< boxVolume(10, 5, 2) << '\n';

return 0;

}

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 20/23

20

Output

$ g++ -Wall -o volume volume.cc

$ volume

The default box volume is: 1

The volume of a box with length 10,

width 1 and height 1 is: 10

The volume of a box with length 10, width 5 and height 1 is: 50

The volume of a box with length 10,

width 5 and height 2 is: 100

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 21/23

21

9 . Function Overloading

In C++, several functions of the same name can bedefined as long as these function name differentsets of parameters (different types or differentnumber of parameters).

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 22/23

22

9 .1 An Example

// Using overloaded functions #include <iostream.h>

int square(int x) { return x * x; }

double square(double y) { return y * y; }

main() {

cout << "The square of integer 7 is " << square(7) << "\nThe square of double 7.5 is " << square(7.5) << '\n';

return 0; }

8/2/2019 Call by Reference

http://slidepdf.com/reader/full/call-by-reference 23/23

23

Output

$ g++ -Wall -o overload overload.cc

$ overload

The square of integer 7 is 49 The square of double 7.5 is 56.25