SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd StudyChris Ohk
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
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"OdessaJS Conf
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
C++ Programming - 14th Study
C++ Programming - 14th StudyC++ Programming - 14th Study
C++ Programming - 14th StudyChris Ohk
 
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 overloadingkinan keshkeh
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 

Was ist angesagt? (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
 

Andere mochten auch

Andere mochten auch (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)
 

Ähnlich wie 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.pptxssuser3cbb4c
 
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 JobyGrejoJoby1
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
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-ee01083101premrings
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentesmfuentessss
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptxNelyJay
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxDeepasCSE
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksBeginning direct3d gameprogrammingcpp02_20160324_jintaeks
Beginning direct3d gameprogrammingcpp02_20160324_jintaeksJinTaek Seo
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 WorkerThousandEyes
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

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