Dinesh Maharjan, 2022
OOP in C++
Basics of C++
Instructor: Dinesh Maharjan
2022
Why C++
Why C++
Why C++
Why C++
Why C++
• Superset of C
• Object Oriented Programming
• All the features of OOP
• High level Programming
• Compiled
• Initially known as C with classes
• Generic Programming Language
• Does not need to specify the type of data before
it is instantiated.
Basics
C++ Basics
C++ First Program
• #include is preprocessor.
• Preprocessor is the program that process the source file
before compilation.
• Std is namespace where different classes and functions are
defined.
• Namespace defines new scopes.
• Main is the function from where program starts to run
• Its return type must be int in c++
• Cout is object of ostream class define in c++ standard.
• It sends output stream to standard output (screen)
• The source file extension is cpp
Hello World
C++ First Program
C++ First Program
C++ Basics
C++ Basics
• Pre-processor process the source code before
compilation takes place
• #include preprocessor includes the specified
header file in the source code
• #define creates the symbolic constant.
• It is macro substitution pre-processor.
• It substitute the defined macro with the
constant in the source code
Pre-processor
• Macro Substitution
• File Inclusion
• Compiler Control Directives
• Line Control
• Error Directives
Preprocessor Types
• #define PI 3.14
• #define INCREMENT ++
• #define BLANK_LINE cout<<”n”
• #define SQARE(x) (x*x)
• #define MAX(x,y) (x>y?x:y)
• #define TWO_PI 2*PI
Macro Subtitution
• # acts as double quotes
• #define STR(x) #x
• Cout<<str(hello) is same as
• Cout<<”hello”
•
• ## concatenates two arguments
• #define GLUE(x,y) x ## y
• Cout<<GLUE(hello, world);
Macro Substitution
• A macro lasts until it is undefined
• #define PI 3.14
• Cout<<PI
• #undef PI
•
Undefining Macro
• Includes parts of code if condition is true
• #ifdef <macro>allows to compile the code between #ifdef and
#endif if macros has been defined using #define
• #include<iostream>
• #define PI 3.14
• Int main(){

#ifdef PI

Int a=10;

#endif

Cout<<a;

Return 0;

}
Compiler control directive
• #ifndef works opposite
• #include<iostream>
• #define PI 3.14
• Int main(){

#ifdef PI

Int a=10;

#endif

Cout<<a;

Return 0;

}
Compiler control directive
• #if, #elif and #else works same as if
• #include<iostream>
• #define PI 3
• Int main(){

Int a;

#if PI>2

A=10;

#elif PI<2

A=3;

#endif

Cout<<a;

Return 0;

}
Compiler control directive
• #defines error message
• #include<iostream>
• #define PI 3.14
• Int main(){

#ifndef PI

#error macro is not defined

#endif

Return 0;

}
Error directive
C++ Basics
• Cerr displays unbuffered error message.
• Cerr is object of stderr
• It can’t be stored for displaying.
• So, it is used whenever we need to display the
message immediately.
• Clog is also object of stderr
• Clog is buffered error message.
• It is stored into buffer before it is displayed on the
screen.
Error Stream
• Int fact(int n){
• Int f=1;
• for(int i=1;i<=n;i++)

f=f*i;
• }
• Return f;
• }
Header File (factorial.h)
• #include<iostream>
• #include “factorial.h”
• Using namespace std;
• Int main(){

Int n;

Cout<<”Enter integer”<<endl;

Cin>>n;

Cout<<”Factorial=”<<fact(n)<<endl;
• }
Header File
(factorialDemo.cpp)
• Reserved Identifiers
• Have their own
purpose
Keywords
• #define PI 3.14
• This substitutes all the PI that appears in
source code with 3.14
• The symbolic constant is all caps
• Symbolic constant created with const is
processed by compiler but the one created
with preprocessor
Symbolic Constant
Data Types
Data Types
• String class makes very easy to handle
strings.
• No need to put n at last of the string
• #include<string>
• String name=“Dan”;
• Cout<<name<<endl;
• Cout<<name[0]<<endl;
• Name[0]=‘T’;
String
• #include<string>
• String name=“Dan”;
• Cout<<name.size();
• Cout<<name.length();
• Cin>>name;
• Char surname[50];
• Cin>surname;
String
String
• #include<iostream>
• #include<string>
• Int main(){

string name;

Cout<<”Enter your name”<<endl;

getline(cin,name);

Cout<<name<<endl;

Return 0;
• }
String with space
Unary Operator
 Prefix Increment : ++a
 example:
 int a=5;
 b=++a; // value of b=6; a=6;
 Postfix Increment: a++
 example
 int a=5;
 b=a++; //value of b=5; a=6;
Operator Precedence
< <= > >= 1
!= == 2
Relational operator has lower priority
than Arithmetic Operator
Operator Precedence
! 1
&& 2
|| 3
• ! has the prefix priority of ++ or –
• && and || has lower priority than
relational operator
Operator Precedence
~ 1
<< >> 2
& 3
^ 4
| 5
• Externdemo.cpp
• #include<iostream>
• #include<string>
• extern int count;
• Int main(){

cout<<count<<endl;

Return 0;
• }
• Helper.cpp
• Int count =10;
• g++ externdemo.cpp helper.cpp -o output
• ./output
Extern example
• Output
• 1
• 2
• 3
Static Variable
• We cannot declare reference variable.
• It must be initialized.
• int &b=a;
• We cannot create reference variable for constant.
• int &b=5; //
• However we can create constant reference to constant.
• const int &b=5;
• const int &b=a+b;
Reference
#include<iostream>
using namespace std;
int main(){
int a=10,&b=a;
cout<<”a= “<<a<<endl;
cout<<”b= “<<b<<endl;
cout<<”Address of b=”<<&b<<endl;
cout<<”Address of a=”<<&a<<endl;
a++;
cout<<”a= “<<a<<endl;
cout<<”b= “<<b<<endl;
}
Reference
Reference vs Pointer
Reference Pointer
Hidden address Exposed address
Cannot point to NULL Can point to NULL
Cannot point to different
variables at different time
Can point to different avariables
at different time
Does not allow to operate on
address
Allow to operate on address
Array of reference is not allowed Array of pointer is allowed
• Defines the format for output
• Iomanip header file is needed
• Output is right justified
• E.g. endl, setw(),setprecision()
• Setw() sets the width for output
• Cout<<setw(10)<<“Nepal”<<endl;
• Cout<<setw(10)<<“Korea”<<endl;
• Cout<<setw(10)<<“Japan”<<endl;
Manipulators
• Setprecision defines the rounding position for
float datatype.
• Cout<<setprecision(1)<<1.43<<endl;
• Cout<<setprecision(2)<<1.45<<endl;
• Cout<<setprecision(3)<<1.56<<endl;
• 1
• 1.4
• 1.56
Manipulators
• Attach names to integral constants.
• Makes program easy to read and maintain.
• Enum color{red,green,blue};
• Cout<<red<<endl;
• Blue of red=0, green=1, blue=2
• Enum color={red,green=5,blue=6};
enum
• Std namespace is defined in iostream library
• Std namespace consists of istream and
ostream class.
• Cout is object of ostream
• Cin is object of istream.
Classes in std namespace
• #include<iostream>
• #include<vector>
• Int main(){

Vector<int> v={1,2,9};

for(int I:v)

Cout<<v<<endl;
• }
New For LOOP
• #include<iostream>
• #include<vector>
• Int main(){

Vector<int> v={1,2,9};

for(int I:v)

Cout<<v<<endl;

Break;
• }
Break LOOP
• #include<iostream>
• #include<vector>
• Int main(){

Vector<int> v={1,2,9};

While (true){

Cout<<v[0]<<endl;

Break;}
• }
Break LOOP
• #include<iostream>
• #include<vector>
• Int main(){

Vector<int> v={1,2,9};

While (true){

Cout<<v[0]<<endl;

continue;}
• }
Skip LOOP

C++Basics.pdf

  • 1.
    Dinesh Maharjan, 2022 OOPin C++ Basics of C++ Instructor: Dinesh Maharjan 2022
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 9.
    • Superset ofC • Object Oriented Programming • All the features of OOP • High level Programming • Compiled • Initially known as C with classes • Generic Programming Language • Does not need to specify the type of data before it is instantiated. Basics
  • 10.
  • 11.
  • 12.
    • #include ispreprocessor. • Preprocessor is the program that process the source file before compilation. • Std is namespace where different classes and functions are defined. • Namespace defines new scopes. • Main is the function from where program starts to run • Its return type must be int in c++ • Cout is object of ostream class define in c++ standard. • It sends output stream to standard output (screen) • The source file extension is cpp Hello World
  • 13.
  • 14.
  • 15.
  • 16.
  • 18.
    • Pre-processor processthe source code before compilation takes place • #include preprocessor includes the specified header file in the source code • #define creates the symbolic constant. • It is macro substitution pre-processor. • It substitute the defined macro with the constant in the source code Pre-processor
  • 19.
    • Macro Substitution •File Inclusion • Compiler Control Directives • Line Control • Error Directives Preprocessor Types
  • 20.
    • #define PI3.14 • #define INCREMENT ++ • #define BLANK_LINE cout<<”n” • #define SQARE(x) (x*x) • #define MAX(x,y) (x>y?x:y) • #define TWO_PI 2*PI Macro Subtitution
  • 21.
    • # actsas double quotes • #define STR(x) #x • Cout<<str(hello) is same as • Cout<<”hello” • • ## concatenates two arguments • #define GLUE(x,y) x ## y • Cout<<GLUE(hello, world); Macro Substitution
  • 22.
    • A macrolasts until it is undefined • #define PI 3.14 • Cout<<PI • #undef PI • Undefining Macro
  • 23.
    • Includes partsof code if condition is true • #ifdef <macro>allows to compile the code between #ifdef and #endif if macros has been defined using #define • #include<iostream> • #define PI 3.14 • Int main(){  #ifdef PI  Int a=10;  #endif  Cout<<a;  Return 0;  } Compiler control directive
  • 24.
    • #ifndef worksopposite • #include<iostream> • #define PI 3.14 • Int main(){  #ifdef PI  Int a=10;  #endif  Cout<<a;  Return 0;  } Compiler control directive
  • 25.
    • #if, #elifand #else works same as if • #include<iostream> • #define PI 3 • Int main(){  Int a;  #if PI>2  A=10;  #elif PI<2  A=3;  #endif  Cout<<a;  Return 0;  } Compiler control directive
  • 26.
    • #defines errormessage • #include<iostream> • #define PI 3.14 • Int main(){  #ifndef PI  #error macro is not defined  #endif  Return 0;  } Error directive
  • 27.
  • 30.
    • Cerr displaysunbuffered error message. • Cerr is object of stderr • It can’t be stored for displaying. • So, it is used whenever we need to display the message immediately. • Clog is also object of stderr • Clog is buffered error message. • It is stored into buffer before it is displayed on the screen. Error Stream
  • 34.
    • Int fact(intn){ • Int f=1; • for(int i=1;i<=n;i++)  f=f*i; • } • Return f; • } Header File (factorial.h)
  • 35.
    • #include<iostream> • #include“factorial.h” • Using namespace std; • Int main(){  Int n;  Cout<<”Enter integer”<<endl;  Cin>>n;  Cout<<”Factorial=”<<fact(n)<<endl; • } Header File (factorialDemo.cpp)
  • 39.
    • Reserved Identifiers •Have their own purpose Keywords
  • 46.
    • #define PI3.14 • This substitutes all the PI that appears in source code with 3.14 • The symbolic constant is all caps • Symbolic constant created with const is processed by compiler but the one created with preprocessor Symbolic Constant
  • 51.
  • 59.
  • 62.
    • String classmakes very easy to handle strings. • No need to put n at last of the string • #include<string> • String name=“Dan”; • Cout<<name<<endl; • Cout<<name[0]<<endl; • Name[0]=‘T’; String
  • 63.
    • #include<string> • Stringname=“Dan”; • Cout<<name.size(); • Cout<<name.length(); • Cin>>name; • Char surname[50]; • Cin>surname; String
  • 64.
  • 65.
    • #include<iostream> • #include<string> •Int main(){  string name;  Cout<<”Enter your name”<<endl;  getline(cin,name);  Cout<<name<<endl;  Return 0; • } String with space
  • 66.
    Unary Operator  PrefixIncrement : ++a  example:  int a=5;  b=++a; // value of b=6; a=6;  Postfix Increment: a++  example  int a=5;  b=a++; //value of b=5; a=6;
  • 72.
    Operator Precedence < <=> >= 1 != == 2 Relational operator has lower priority than Arithmetic Operator
  • 73.
    Operator Precedence ! 1 &&2 || 3 • ! has the prefix priority of ++ or – • && and || has lower priority than relational operator
  • 74.
  • 84.
    • Externdemo.cpp • #include<iostream> •#include<string> • extern int count; • Int main(){  cout<<count<<endl;  Return 0; • } • Helper.cpp • Int count =10; • g++ externdemo.cpp helper.cpp -o output • ./output Extern example
  • 87.
    • Output • 1 •2 • 3 Static Variable
  • 92.
    • We cannotdeclare reference variable. • It must be initialized. • int &b=a; • We cannot create reference variable for constant. • int &b=5; // • However we can create constant reference to constant. • const int &b=5; • const int &b=a+b; Reference
  • 93.
    #include<iostream> using namespace std; intmain(){ int a=10,&b=a; cout<<”a= “<<a<<endl; cout<<”b= “<<b<<endl; cout<<”Address of b=”<<&b<<endl; cout<<”Address of a=”<<&a<<endl; a++; cout<<”a= “<<a<<endl; cout<<”b= “<<b<<endl; } Reference
  • 94.
    Reference vs Pointer ReferencePointer Hidden address Exposed address Cannot point to NULL Can point to NULL Cannot point to different variables at different time Can point to different avariables at different time Does not allow to operate on address Allow to operate on address Array of reference is not allowed Array of pointer is allowed
  • 95.
    • Defines theformat for output • Iomanip header file is needed • Output is right justified • E.g. endl, setw(),setprecision() • Setw() sets the width for output • Cout<<setw(10)<<“Nepal”<<endl; • Cout<<setw(10)<<“Korea”<<endl; • Cout<<setw(10)<<“Japan”<<endl; Manipulators
  • 96.
    • Setprecision definesthe rounding position for float datatype. • Cout<<setprecision(1)<<1.43<<endl; • Cout<<setprecision(2)<<1.45<<endl; • Cout<<setprecision(3)<<1.56<<endl; • 1 • 1.4 • 1.56 Manipulators
  • 97.
    • Attach namesto integral constants. • Makes program easy to read and maintain. • Enum color{red,green,blue}; • Cout<<red<<endl; • Blue of red=0, green=1, blue=2 • Enum color={red,green=5,blue=6}; enum
  • 105.
    • Std namespaceis defined in iostream library • Std namespace consists of istream and ostream class. • Cout is object of ostream • Cin is object of istream. Classes in std namespace
  • 112.
    • #include<iostream> • #include<vector> •Int main(){  Vector<int> v={1,2,9};  for(int I:v)  Cout<<v<<endl; • } New For LOOP
  • 113.
    • #include<iostream> • #include<vector> •Int main(){  Vector<int> v={1,2,9};  for(int I:v)  Cout<<v<<endl;  Break; • } Break LOOP
  • 114.
    • #include<iostream> • #include<vector> •Int main(){  Vector<int> v={1,2,9};  While (true){  Cout<<v[0]<<endl;  Break;} • } Break LOOP
  • 115.
    • #include<iostream> • #include<vector> •Int main(){  Vector<int> v={1,2,9};  While (true){  Cout<<v[0]<<endl;  continue;} • } Skip LOOP