SlideShare uma empresa Scribd logo
1 de 23
Introduction to C++



Noppadon Kamolvilassatian

Department of Computer Engineering
Prince of Songkla University
                                     1
Contents

s   1. Introduction
s   2. C++ Single-Line Comments
s   3. C++ Stream Input/Output
s   4. Declarations in C++
s   5. Creating New Data Types in C++
s   6. Reference Parameters
s   7. Const Qualifier
s   8. Default Arguments
s   9. Function Overloading             2
1. Introduction

s   C++ improves on many of C’s features.
s   C++ provides object-oriented programming
    (OOP).
s   C++ is a superset to C.
s   No ANSI standard exists yet (in 1994).




                                               3
2. C++ Single-Line Comments

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

// This is a single-line comment.




                                       4
3. C++ Stream Input/Output

s   In C,
    printf(“Enter new tag: “);
    scanf(“%d”, &tag);
    printf(“The new tag is: %dn”, tag);
s   In C++,
    cout << “Enter new tag: “;
    cin >> tag;
    cout << “The new tag is : “ << tag << ‘n’;

                                                  5
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;
                                         6
if (myAge > friendsAge)
        cout << "You are older.n";
     else
        if (myAge < friendsAge)
           cout << "You are younger.n";
        else
           cout << "You and your friend are the
    same age.n";

    return 0;
}

                                                  7
4. Declarations in C++

s   In C++, declarations can be placed anywhere
    (except in the condition of a while, do/while, f
    or or if structure.)
s   An example
cout << “Enter two integers: “;
int x, y;
cin >> x >> y;
cout << “The sum of “ << x << “ and “ << y
     << “ is “ << x + y << ‘n’;


                                                   8
s   Another example

for (int i = 0; i <= 5; i++)
    cout << i << ‘n’;




                               9
5. Creating New Data Types in C++

struct Name {
     char first[10];
     char last[10];
};
s   In C,
struct Name stdname;
s   In C++,
Name stdname;
s   The same is true for enums and unions
                                            10
6. Reference Parameters

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

s   Reference parameters allows function arguments
    to be changed without using return or pointers.




                                                      11
6.1 Comparing Call by Value, Call by Reference
with 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 sqrByValn"
        <<   "Value returned by sqrByVal: "
        <<   sqrByVal(x)
        <<   "nx = " << x << " after sqrByValnn";
                                                 12
cout << "y = " << y << " before sqrByPointern";
    sqrByPointer(&y);
    cout << "y = " << y << " after sqrByPointernn";


     cout << "z = " << z << " before sqrByRefn";
    sqrByRef(z);
    cout << "z = " << z << " after sqrByRefn";

    return 0;
}



                                                    13
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
}

                                        14
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
                                  15
7. The Const Qualifier

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


s   The const variables must be initialized when
    declared.




                                                       16
8. Default Arguments

s   When a default argument is omitted in a function
    call, the default value of that argument is automati
    cally passed in the call.
s   Default arguments must be the rightmost (trailing)
    arguments.




                                                      17
8.1 An Example

// Using default arguments
#include <iostream.h>

// Calculate the volume of   a box
int boxVolume(int length =   1, int width = 1,
              int height =   1)
   { return length * width   * height; }




                                                 18
main()
{
   cout <<   "The default box volume is: "
        <<   boxVolume()
        <<   "nnThe volume of a box with length 10,n"
        <<   "width 1 and height 1 is: "
        <<   boxVolume(10)
        <<   "nnThe volume of a box with length 10,n"
        <<   "width 5 and height 1 is: "
        <<   boxVolume(10, 5)
        <<   "nnThe volume of a box with length 10,n"
        <<   "width 5 and height 2 is: "
        <<   boxVolume(10, 5, 2)
        <<   'n';

    return 0;
}
                                                           19
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          20
9. Function Overloading

s   In C++, several functions of the same name can be
    defined as long as these function name different
    sets of parameters (different types or different nu
    mber of parameters).




                                                     21
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;
 }
                                               22
Output

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

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




                                      23

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th Study
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
P1
P1P1
P1
 
Pointers
PointersPointers
Pointers
 
C++ programs
C++ programsC++ programs
C++ programs
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 

Destaque (12)

Database
DatabaseDatabase
Database
 
Stroustrup c++0x overview
Stroustrup c++0x overviewStroustrup c++0x overview
Stroustrup c++0x overview
 
Os
OsOs
Os
 
Mem hierarchy
Mem hierarchyMem hierarchy
Mem hierarchy
 
Curves2
Curves2Curves2
Curves2
 
types of operating system
types of operating systemtypes of operating system
types of operating system
 
Operating system and its function
Operating system and its functionOperating system and its function
Operating system and its function
 
Types of operating system
Types of operating systemTypes of operating system
Types of operating system
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Presentation on operating system
 Presentation on operating system Presentation on operating system
Presentation on operating system
 
Operating system overview concepts ppt
Operating system overview concepts pptOperating system overview concepts ppt
Operating system overview concepts ppt
 
Operating system.ppt (1)
Operating system.ppt (1)Operating system.ppt (1)
Operating system.ppt (1)
 

Semelhante a Oop1

C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
mfuentessss
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
Syed Umair
 

Semelhante a Oop1 (20)

C++ file
C++ fileC++ file
C++ file
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++
C++C++
C++
 
Project in programming
Project in programmingProject in programming
Project in programming
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++
 
Managing console
Managing consoleManaging console
Managing console
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
 

Mais de Vaibhav Bajaj (19)

P smile
P smileP smile
P smile
 
Ppt history-of-apple2203 (1)
Ppt history-of-apple2203 (1)Ppt history-of-apple2203 (1)
Ppt history-of-apple2203 (1)
 
C++0x
C++0xC++0x
C++0x
 
Blu ray disc slides
Blu ray disc slidesBlu ray disc slides
Blu ray disc slides
 
Assembler
AssemblerAssembler
Assembler
 
Assembler (2)
Assembler (2)Assembler (2)
Assembler (2)
 
Projection of solids
Projection of solidsProjection of solids
Projection of solids
 
Projection of planes
Projection of planesProjection of planes
Projection of planes
 
Ortographic projection
Ortographic projectionOrtographic projection
Ortographic projection
 
Isometric
IsometricIsometric
Isometric
 
Intersection 1
Intersection 1Intersection 1
Intersection 1
 
Important q
Important qImportant q
Important q
 
Eg o31
Eg o31Eg o31
Eg o31
 
Development of surfaces of solids
Development of surfaces of solidsDevelopment of surfaces of solids
Development of surfaces of solids
 
Development of surfaces of solids copy
Development of surfaces of solids   copyDevelopment of surfaces of solids   copy
Development of surfaces of solids copy
 
Curve1
Curve1Curve1
Curve1
 
Cad
CadCad
Cad
 
Cad notes
Cad notesCad notes
Cad notes
 
Scales
ScalesScales
Scales
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Oop1

  • 1. Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
  • 2. Contents s 1. Introduction s 2. C++ Single-Line Comments s 3. C++ Stream Input/Output s 4. Declarations in C++ s 5. Creating New Data Types in C++ s 6. Reference Parameters s 7. Const Qualifier s 8. Default Arguments s 9. Function Overloading 2
  • 3. 1. Introduction s C++ improves on many of C’s features. s C++ provides object-oriented programming (OOP). s C++ is a superset to C. s No ANSI standard exists yet (in 1994). 3
  • 4. 2. C++ Single-Line Comments s In C, /* This is a single-line comment. */ s In C++, // This is a single-line comment. 4
  • 5. 3. C++ Stream Input/Output s In C, printf(“Enter new tag: “); scanf(“%d”, &tag); printf(“The new tag is: %dn”, tag); s In C++, cout << “Enter new tag: “; cin >> tag; cout << “The new tag is : “ << tag << ‘n’; 5
  • 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; 6
  • 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 the same age.n"; return 0; } 7
  • 8. 4. Declarations in C++ s In C++, declarations can be placed anywhere (except in the condition of a while, do/while, f or or if structure.) s An example cout << “Enter two integers: “; int x, y; cin >> x >> y; cout << “The sum of “ << x << “ and “ << y << “ is “ << x + y << ‘n’; 8
  • 9. s Another example for (int i = 0; i <= 5; i++) cout << i << ‘n’; 9
  • 10. 5. Creating New Data Types in C++ struct Name { char first[10]; char last[10]; }; s In C, struct Name stdname; s In C++, Name stdname; s The same is true for enums and unions 10
  • 11. 6. Reference Parameters s In C, all function calls are call by value. – Call be reference is simulated using pointers s Reference parameters allows function arguments to be changed without using return or pointers. 11
  • 12. 6.1 Comparing Call by Value, Call by Reference with 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 sqrByValn" << "Value returned by sqrByVal: " << sqrByVal(x) << "nx = " << x << " after sqrByValnn"; 12
  • 13. cout << "y = " << y << " before sqrByPointern"; sqrByPointer(&y); cout << "y = " << y << " after sqrByPointernn"; cout << "z = " << z << " before sqrByRefn"; sqrByRef(z); cout << "z = " << z << " after sqrByRefn"; return 0; } 13
  • 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 } 14
  • 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 15
  • 16. 7. The Const Qualifier s Used to declare “constant variables” (instead of #define) const float PI = 3.14156; s The const variables must be initialized when declared. 16
  • 17. 8. Default Arguments s When a default argument is omitted in a function call, the default value of that argument is automati cally passed in the call. s Default arguments must be the rightmost (trailing) arguments. 17
  • 18. 8.1 An Example // Using default arguments #include <iostream.h> // Calculate the volume of a box int boxVolume(int length = 1, int width = 1, int height = 1) { return length * width * height; } 18
  • 19. main() { cout << "The default box volume is: " << boxVolume() << "nnThe volume of a box with length 10,n" << "width 1 and height 1 is: " << boxVolume(10) << "nnThe volume of a box with length 10,n" << "width 5 and height 1 is: " << boxVolume(10, 5) << "nnThe volume of a box with length 10,n" << "width 5 and height 2 is: " << boxVolume(10, 5, 2) << 'n'; return 0; } 19
  • 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 20
  • 21. 9. Function Overloading s In C++, several functions of the same name can be defined as long as these function name different sets of parameters (different types or different nu mber of parameters). 21
  • 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; } 22
  • 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 23