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)

DOCX
C++ file
simarsimmygrewal
 
PPTX
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
PDF
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
PPTX
Oops presentation
sushamaGavarskar1
 
PPTX
lesson 2.pptx
khaledahmed316
 
PPTX
CSE107JAN2023_KMS_Intrnjononolo+CPP.pptx
alaminbuetcse22
 
DOCX
C++ file
Mukund Trivedi
 
DOCX
C++ file
Mukund Trivedi
 
DOC
Pads lab manual final
AhalyaR
 
DOCX
C++
Raj vardhan
 
PPTX
Project in programming
sahashi11342091
 
PPT
C++ Language
Syed Zaid Irshad
 
PPTX
Computational PhysicsssComputational Physics.pptx
khanzasad009
 
PPTX
CSC2161Programming_in_Cpp_Lecture notes.pptx
winebaldbanituze
 
PDF
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
PPTX
Programming - Marla Fuentes
mfuentessss
 
PPTX
Lecture 9_Classes.pptx
NelyJay
 
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
PPTX
Blazing Fast Windows 8 Apps using Visual C++
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
Oops presentation
sushamaGavarskar1
 
lesson 2.pptx
khaledahmed316
 
CSE107JAN2023_KMS_Intrnjononolo+CPP.pptx
alaminbuetcse22
 
C++ file
Mukund Trivedi
 
C++ file
Mukund Trivedi
 
Pads lab manual final
AhalyaR
 
Project in programming
sahashi11342091
 
C++ Language
Syed Zaid Irshad
 
Computational PhysicsssComputational Physics.pptx
khanzasad009
 
CSC2161Programming_in_Cpp_Lecture notes.pptx
winebaldbanituze
 
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
Programming - Marla Fuentes
mfuentessss
 
Lecture 9_Classes.pptx
NelyJay
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
Blazing Fast Windows 8 Apps using Visual C++
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
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)

PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 

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