SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PDF
Data Structure - 2nd Study
Chris Ohk
 
PDF
C++ TUTORIAL 8
Farhan Ab Rahman
 
PDF
C++ Programming - 4th Study
Chris Ohk
 
PDF
C++ TUTORIAL 6
Farhan Ab Rahman
 
PDF
C++ TUTORIAL 2
Farhan Ab Rahman
 
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
OdessaJS Conf
 
PDF
C++ TUTORIAL 9
Farhan Ab Rahman
 
DOCX
Basic Programs of C++
Bharat Kalia
 
PDF
C++ Programming - 2nd Study
Chris Ohk
 
DOCX
Oop lab report
khasmanjalali
 
PDF
C++ Programming - 14th Study
Chris Ohk
 
PPTX
C++ Lambda and concurrency
명신 김
 
PDF
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
PDF
Pointers
Hitesh Wagle
 
PDF
C++ programs
Mukund Gandrakota
 
DOC
oop Lecture 4
Anwar Ul Haq
 
PPTX
Operator overloading2
zindadili
 
DOC
Ds 2 cycle
Chaitanya Kn
 
PDF
C++ Programming - 1st Study
Chris Ohk
 
Data Structure - 2nd Study
Chris Ohk
 
C++ TUTORIAL 8
Farhan Ab Rahman
 
C++ Programming - 4th Study
Chris Ohk
 
C++ TUTORIAL 6
Farhan Ab Rahman
 
C++ TUTORIAL 2
Farhan Ab Rahman
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
OdessaJS Conf
 
C++ TUTORIAL 9
Farhan Ab Rahman
 
Basic Programs of C++
Bharat Kalia
 
C++ Programming - 2nd Study
Chris Ohk
 
Oop lab report
khasmanjalali
 
C++ Programming - 14th Study
Chris Ohk
 
C++ Lambda and concurrency
명신 김
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
Pointers
Hitesh Wagle
 
C++ programs
Mukund Gandrakota
 
oop Lecture 4
Anwar Ul Haq
 
Operator overloading2
zindadili
 
Ds 2 cycle
Chaitanya Kn
 
C++ Programming - 1st Study
Chris Ohk
 

Viewers also liked (12)

PPT
Database
Vaibhav Bajaj
 
PPT
Stroustrup c++0x overview
Vaibhav Bajaj
 
PPT
Mem hierarchy
Vaibhav Bajaj
 
PPT
Curves2
Vaibhav Bajaj
 
PPTX
types of operating system
Mahira Rashdi
 
PPTX
Operating system and its function
Nikhi Jain
 
PPTX
Types of operating system
Jesus Obenita Jr.
 
PPTX
Operating Systems
Harshith Meela
 
PPT
Presentation on operating system
Nitish Xavier Tirkey
 
PPTX
Operating system overview concepts ppt
RajendraPrasad Alladi
 
PPT
Operating system.ppt (1)
Vaibhav Bajaj
 
Database
Vaibhav Bajaj
 
Stroustrup c++0x overview
Vaibhav Bajaj
 
Mem hierarchy
Vaibhav Bajaj
 
Curves2
Vaibhav Bajaj
 
types of operating system
Mahira Rashdi
 
Operating system and its function
Nikhi Jain
 
Types of operating system
Jesus Obenita Jr.
 
Operating Systems
Harshith Meela
 
Presentation on operating system
Nitish Xavier Tirkey
 
Operating system overview concepts ppt
RajendraPrasad Alladi
 
Operating system.ppt (1)
Vaibhav Bajaj
 
Ad

Similar to Oop1 (20)

PDF
C++primer
leonlongli
 
PPT
C++ Language
Syed Zaid Irshad
 
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
PPTX
Fundamental of programming Fundamental of programming
LidetAdmassu
 
PPTX
Intro to c++
temkin abdlkader
 
PPTX
Functions in C++ programming language.pptx
rebin5725
 
PPT
Lecture#7 Call by value and reference in c++
NUST Stuff
 
PPT
2.overview of c++ ________lecture2
Warui Maina
 
PPT
Lecture05
elearning_portal
 
PDF
C++ L01-Variables
Mohammad Shaker
 
PPT
Pengaturcaraan asas
Unit Kediaman Luar Kampus
 
PDF
Chap 5 c++
Venkateswarlu Vuggam
 
PPT
Elementary_Of_C++_Programming_Language.ppt
GordanaJovanoska1
 
DOCX
C++ Tutorial.docx
PinkiVats1
 
PPTX
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
PPT
Chap 5 c++
Venkateswarlu Vuggam
 
PPT
Chapter 2 Introduction to C++
GhulamHussain142878
 
PPTX
C++ FUNCTIONS-1.pptx
ShashiShash2
 
PPTX
C_plus_plus
Ralph Weber
 
PPTX
Programming Fundamentals lecture-10.pptx
singyali199
 
C++primer
leonlongli
 
C++ Language
Syed Zaid Irshad
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Intro to c++
temkin abdlkader
 
Functions in C++ programming language.pptx
rebin5725
 
Lecture#7 Call by value and reference in c++
NUST Stuff
 
2.overview of c++ ________lecture2
Warui Maina
 
Lecture05
elearning_portal
 
C++ L01-Variables
Mohammad Shaker
 
Pengaturcaraan asas
Unit Kediaman Luar Kampus
 
Elementary_Of_C++_Programming_Language.ppt
GordanaJovanoska1
 
C++ Tutorial.docx
PinkiVats1
 
COMPUTER PROGRAMMING LANGUAGE C++ 1.pptx
atokoosetaiwoboluwat
 
Chapter 2 Introduction to C++
GhulamHussain142878
 
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C_plus_plus
Ralph Weber
 
Programming Fundamentals lecture-10.pptx
singyali199
 
Ad

More from Vaibhav Bajaj (19)

PPT
P smile
Vaibhav Bajaj
 
PPT
Ppt history-of-apple2203 (1)
Vaibhav Bajaj
 
PDF
C++0x
Vaibhav Bajaj
 
PPT
Blu ray disc slides
Vaibhav Bajaj
 
PPT
Assembler
Vaibhav Bajaj
 
PPT
Assembler (2)
Vaibhav Bajaj
 
PPT
Projection of solids
Vaibhav Bajaj
 
PPT
Projection of planes
Vaibhav Bajaj
 
PPT
Ortographic projection
Vaibhav Bajaj
 
PPT
Isometric
Vaibhav Bajaj
 
PPT
Intersection 1
Vaibhav Bajaj
 
DOC
Important q
Vaibhav Bajaj
 
DOC
Eg o31
Vaibhav Bajaj
 
PPT
Development of surfaces of solids
Vaibhav Bajaj
 
PPT
Development of surfaces of solids copy
Vaibhav Bajaj
 
PPT
Curve1
Vaibhav Bajaj
 
DOC
Cad notes
Vaibhav Bajaj
 
PPT
Scales
Vaibhav Bajaj
 
P smile
Vaibhav Bajaj
 
Ppt history-of-apple2203 (1)
Vaibhav Bajaj
 
Blu ray disc slides
Vaibhav Bajaj
 
Assembler
Vaibhav Bajaj
 
Assembler (2)
Vaibhav Bajaj
 
Projection of solids
Vaibhav Bajaj
 
Projection of planes
Vaibhav Bajaj
 
Ortographic projection
Vaibhav Bajaj
 
Isometric
Vaibhav Bajaj
 
Intersection 1
Vaibhav Bajaj
 
Important q
Vaibhav Bajaj
 
Development of surfaces of solids
Vaibhav Bajaj
 
Development of surfaces of solids copy
Vaibhav Bajaj
 
Cad notes
Vaibhav Bajaj
 

Recently uploaded (20)

PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PPTX
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
PDF
Basics of Electronics for IOT(actuators ,microcontroller etc..)
arnavmanesh
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Alpha Altcoin Setup : TIA - 19th July 2025
CIFDAQ
 
PDF
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
PPTX
Machine Learning Benefits Across Industries
SynapseIndia
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PCU Keynote at IEEE World Congress on Services 250710.pptx
Ramesh Jain
 
Basics of Electronics for IOT(actuators ,microcontroller etc..)
arnavmanesh
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Alpha Altcoin Setup : TIA - 19th July 2025
CIFDAQ
 
Integrating IIoT with SCADA in Oil & Gas A Technical Perspective.pdf
Rejig Digital
 
Machine Learning Benefits Across Industries
SynapseIndia
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Market Wrap for 18th July 2025 by CIFDAQ
CIFDAQ
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 

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