SlideShare a Scribd company logo
The C++ Language
By Hamza asif
OVERVIEW OF ‘C++’
 Bjarne Stroupstrup, the language’s creator
 C++ was designed to provide Simula’s facilities for program organization
together with C’s efficiency and flexibility for systems programming.
 Modern C and C++ are siblings
OUTLINE
 C++ basic features
 Programming paradigm and statement syntax
 Class definitions
 Data members, methods, constructor, destructor
 Pointers, arrays, and strings
 Parameter passing in functions
 Templates
 Friend
 Operator overloading
 I/O streams
 An example on file copy
 Makefile
C++ FEATURES
 Classes
 User-defined types
 Operator overloading
 Attach different meaning to expressions such as a + b
 References
 Pass-by-reference function arguments
 Virtual Functions
 Dispatched depending on type at run time
 Templates
 Macro-like polymorphism for containers (e.g., arrays)
 Exceptions
COMPILING AND LINKING
 A C++ program consists of one or more source files.
 Source files contain function and class declarations and definitions.
 Files that contain only declarations are incorporated into the source files that
need them when they are compiled.
 Thus they are called include files.
 Files that contain definitions are translated by the compiler into an intermediate
form called object files.
 One or more object files are combined with to form the executable file by the
linker.
A SIMPLE C++ PROGRAM
#include <cstdlib>
#include <iostream>
using namespace std;
int main ( ) {
intx, y;
cout << “Please enter two numbers:”;
cin >> x >> y;
int sum = x + y;
cout << “Their sum is “ << sum << endl;
return EXIT_SUCCESS;
}
THE #INCLUDE DIRECTIVE
 The first two lines:
#include <iostream>
#include <cstdlib>
incorporate the declarations of the iostream and cstdlib libraries
into the source code.
 If your program is going to use a member of the standard library,
the appropriate header file must be included at the beginning of the
source code file.
THE USING STATEMENT
 The line
using namespace std;
tells the compiler to make all names in the predefined namespace std available.
 The C++ standard library is defined within this namespace.
 Incorporating the statement
using namespace std;
is an easy way to get access to the standard library.
 But, it can lead to complications in larger programs.
 This is done with individual using declarations.
using std::cin;
using std::cout;
using std::string;
using std::getline;
COMPILING AND EXECUTING
 The command to compile is dependent upon the compiler and
operating system.
 For the gcc compiler (popular on Linux) the command would be:
 g++ -o HelloWorld HelloWorld.cpp
 For the Microsoft compiler the command would be:
 cl /EHsc HelloWorld.cpp
 To execute the program you would then issue the command
 HelloWorld
C++ DATA TYPE
 Basic Java types such as int, double, char have C++ counterparts of
the same name, but there are a few differences:
 Boolean is bool in C++. In C++, 0 means false and anything else
means true.
 C++ has a string class (use string library) and character arrays (but
they behave differently).
CONSTANTS
 Numeric Constants:
 1234 is an int
 1234U or 1234u is an unsigned int
 1234L or 1234l is a long
 1234UL or 1234ul is an unsigned long
 1.234 is a double
 1.234F or 1.234f is a float
 1.234L or 1.234l is a long double.
 Character Constants:
 The form 'c' is a character constant.
 The form 'xhh' is a character constant, where hh is a hexadecimal digit, and hh is between 00 and 7F.
 The form 'x' where x is one of the following is a character constant.
 String Constants:
 The form "sequence of characters“ where sequence of characters does not include ‘"’ is called a string
constant.
 Note escape sequences may appear in the sequence of characters.
 String constants are stored in the computer as arrays of characters followed by a '0'.
OPERATORS
 Bitwise Operators
 ~ (complement)
 & (bitwise and)
 ^ (bitwise exclusive or)
 | (bitwise or)
 << (shift left)
 >> (shift right)
 Assignment Operators
 +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
 Other Operators
 :: (scope resolution)
 ?: (conditional expression)
 stream << var
 stream >> exp
INCREMENT AND DECREMENT
 Prefix:
 ++x
 x is replaced by x+1, and the value is x+1
 --x
 x is replaced by x-1, and the value is x-1
 Postfix:
 x++
 x is replaced by x+1, but the value is x
 x--
 x is replaced by x-1, but the value is x
OBJECT-ORIENTED CONCEPT (C++)
• Objects of the program interact by sending messages to each
other
BASIC C++
 Inherit all C syntax
 Primitive data types
 Supported data types: int, long, short, float, double, char,
bool, and enum
 The size of data types is platform-dependent
 Basic expression syntax
 Defining the usual arithmetic and logical operations such as
+, -, /, %, *, &&, !, and ||
 Defining bit-wise operations, such as &, |, and ~
 Basic statement syntax
 If-else, for, while, and do-while
BASIC C++ (CONT)
 Add a new comment mark
 // For 1 line comment
 /*… */ for a group of line comment
 New data type
 Reference data type “&”. Much likes pointer
int ix; /* ix is "real" variable */
int & rx = ix; /* rx is "alias" for ix */
ix = 1; /* also rx == 1 */
rx = 2; /* also ix == 2 */
 const support for constant declaration, just likes C
CLASS DEFINITIONS
 A C++ class consists of data members and methods (member
functions).
class IntCell
{
public:
explicit IntCell( int initialValue = 0 )
: storedValue( initialValue ) {}
int read( ) const
{ return storedValue;}
void write( int x )
{ storedValue = x; }
private:
int storedValue;
}
Avoid implicit type conversion
Initializer list: used to initialize the data
members directly.
Member functions
Indicates that the member’s invocation does
not change any of the data members.
Data member(s)
INFORMATION HIDING IN C++
 Two labels: public and private
 Determine visibility of class members
 A member that is public may be accessed by any
method in any class
 A member that is private may only be accessed by
methods in its class
 Information hiding
 Data members are declared private, thus restricting
access to internal details of the class
 Methods intended for general use are made public
CONSTRUCTORS
 A constructor is a special method that
describes how an instance of the class (called
object) is constructed
 Whenever an instance of the class is created,
its constructor is called.
 C++ provides a default constructor for each
class, which is a constructor with no
parameters. But, one can define multiple
constructors for the same class, and may
even redefine the default constructor
DESTRUCTOR
 A destructor is called when an object is deleted either
implicitly, or explicitly (using the delete operation)
 The destructor is called whenever an object goes out of
scope or is subjected to a delete.
 Typically, the destructor is used to free up any resources
that were allocated during the use of the object
 C++ provides a default destructor for each class
 The default simply applies the destructor on each data
member. But we can redefine the destructor of a class. A
C++ class can have only one destructor.
 One can redefine the destructor of a class.
 A C++ class can have only one destructor
CONSTRUCTOR AND DESTRUCTOR
class Point {
private :
int _x, _y;
public:
Point() {
_x = _y = 0;
}
Point(const int x, const int y);
Point(const Point &from);
~Point() {void}
void setX(const int val);
void setY(const int val);
int getX() { return _x; }
int getY() { return _y; }
};
CONSTRUCTOR AND DESTRUCTOR
Point::Point(const int x, const int y) :
_x(x), _y(y) {
}
Point::Point(const Point &from) {
_x = from._x;
_y = from._y;
}
Point::~Point(void) {
/* nothing to do */
}
C++ OPERATOR OVERLOADING
class Complex {
...
public:
...
Complex operator +(const Complex &op) {
double real = _real + op._real,
imag = _imag + op._imag;
return(Complex(real, imag));
}
...
};
In this case, we have made operator + a member of class
Complex. An expression of the form
c = a + b;
is translated into a method call
c = a.operator +(a, b);
OPERATOR OVERLOADING
 The overloaded operator may not be a member of a class: It can rather
defined outside the class as a normal overloaded function. For example, we
could define operator + in this way:
class Complex {
...
public:
...
double real() { return _real; }
double imag() { return _imag; }
// No need to define operator here!
};
Complex operator +(Complex &op1, Complex &op2)
{
double real = op1.real() + op2.real(),
imag = op1.imag() + op2.imag();
return(Complex(real, imag));
}
FRIEND
 We can define functions or classes to be friends of a class to
allow them direct access to its private data members
class Complex {
...
public:
...
friend Complex operator +(
const Complex &,
const Complex &
);
};
Complex operator +(const Complex &op1, const Complex &op2) {
double real = op1._real + op2._real,
imag = op1._imag + op2._imag;
return(Complex(real, imag));
}
STANDARD INPUT/OUTPUT STREAMS
 Stream is a sequence of characters
 Working with cin and cout
 Streams convert internal representations to
character streams
 >> input operator (extractor)
 << output operator (inserter)
Thank You

More Related Content

What's hot

Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
Abou Bakr Ashraf
 
C++
C++C++
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
 
Basic c#
Basic c#Basic c#
Basic c#
kishore4268
 
C++ theory
C++ theoryC++ theory
C++ theory
Shyam Khant
 
C Theory
C TheoryC Theory
C Theory
Shyam Khant
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
rehan16091997
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
arshpreetkaur07
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala
jatin batra
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
Ajit Nayak
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
Ralph Weber
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
guestf0562b
 

What's hot (20)

Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
C++
C++C++
C++
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Basic c#
Basic c#Basic c#
Basic c#
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C Theory
C TheoryC Theory
C Theory
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala45 Days C++ Programming Language Training in Ambala
45 Days C++ Programming Language Training in Ambala
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Advanced Programming C++
Advanced Programming C++Advanced Programming C++
Advanced Programming C++
 

Similar to C++ language

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
SukhpreetSingh519414
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
somu rajesh
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chris Adamson
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
SajalKesharwani2
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
shubhra chauhan
 
C++primer
C++primerC++primer
C++primer
leonlongli
 
L6
L6L6
L6
lksoo
 
Bc0037
Bc0037Bc0037
Bc0037
hayerpa
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
Govt. P.G. College Dharamshala
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
Ralph Weber
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
 
C++
C++C++
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
Sayed Ahmed
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
Sayed Ahmed
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
abdulhaq467432
 

Similar to C++ language (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
C++primer
C++primerC++primer
C++primer
 
L6
L6L6
L6
 
Bc0037
Bc0037Bc0037
Bc0037
 
Introduction of C++ By Pawan Thakur
Introduction of C++ By Pawan ThakurIntroduction of C++ By Pawan Thakur
Introduction of C++ By Pawan Thakur
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
7.-Download_CS201-Solved-Subjective-with-Reference-by-Aqib.doc
 

Recently uploaded

South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 

Recently uploaded (20)

South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 

C++ language

  • 1. The C++ Language By Hamza asif
  • 2. OVERVIEW OF ‘C++’  Bjarne Stroupstrup, the language’s creator  C++ was designed to provide Simula’s facilities for program organization together with C’s efficiency and flexibility for systems programming.  Modern C and C++ are siblings
  • 3. OUTLINE  C++ basic features  Programming paradigm and statement syntax  Class definitions  Data members, methods, constructor, destructor  Pointers, arrays, and strings  Parameter passing in functions  Templates  Friend  Operator overloading  I/O streams  An example on file copy  Makefile
  • 4. C++ FEATURES  Classes  User-defined types  Operator overloading  Attach different meaning to expressions such as a + b  References  Pass-by-reference function arguments  Virtual Functions  Dispatched depending on type at run time  Templates  Macro-like polymorphism for containers (e.g., arrays)  Exceptions
  • 5. COMPILING AND LINKING  A C++ program consists of one or more source files.  Source files contain function and class declarations and definitions.  Files that contain only declarations are incorporated into the source files that need them when they are compiled.  Thus they are called include files.  Files that contain definitions are translated by the compiler into an intermediate form called object files.  One or more object files are combined with to form the executable file by the linker.
  • 6. A SIMPLE C++ PROGRAM #include <cstdlib> #include <iostream> using namespace std; int main ( ) { intx, y; cout << “Please enter two numbers:”; cin >> x >> y; int sum = x + y; cout << “Their sum is “ << sum << endl; return EXIT_SUCCESS; }
  • 7. THE #INCLUDE DIRECTIVE  The first two lines: #include <iostream> #include <cstdlib> incorporate the declarations of the iostream and cstdlib libraries into the source code.  If your program is going to use a member of the standard library, the appropriate header file must be included at the beginning of the source code file.
  • 8. THE USING STATEMENT  The line using namespace std; tells the compiler to make all names in the predefined namespace std available.  The C++ standard library is defined within this namespace.  Incorporating the statement using namespace std; is an easy way to get access to the standard library.  But, it can lead to complications in larger programs.  This is done with individual using declarations. using std::cin; using std::cout; using std::string; using std::getline;
  • 9. COMPILING AND EXECUTING  The command to compile is dependent upon the compiler and operating system.  For the gcc compiler (popular on Linux) the command would be:  g++ -o HelloWorld HelloWorld.cpp  For the Microsoft compiler the command would be:  cl /EHsc HelloWorld.cpp  To execute the program you would then issue the command  HelloWorld
  • 10. C++ DATA TYPE  Basic Java types such as int, double, char have C++ counterparts of the same name, but there are a few differences:  Boolean is bool in C++. In C++, 0 means false and anything else means true.  C++ has a string class (use string library) and character arrays (but they behave differently).
  • 11. CONSTANTS  Numeric Constants:  1234 is an int  1234U or 1234u is an unsigned int  1234L or 1234l is a long  1234UL or 1234ul is an unsigned long  1.234 is a double  1.234F or 1.234f is a float  1.234L or 1.234l is a long double.  Character Constants:  The form 'c' is a character constant.  The form 'xhh' is a character constant, where hh is a hexadecimal digit, and hh is between 00 and 7F.  The form 'x' where x is one of the following is a character constant.  String Constants:  The form "sequence of characters“ where sequence of characters does not include ‘"’ is called a string constant.  Note escape sequences may appear in the sequence of characters.  String constants are stored in the computer as arrays of characters followed by a '0'.
  • 12. OPERATORS  Bitwise Operators  ~ (complement)  & (bitwise and)  ^ (bitwise exclusive or)  | (bitwise or)  << (shift left)  >> (shift right)  Assignment Operators  +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=  Other Operators  :: (scope resolution)  ?: (conditional expression)  stream << var  stream >> exp
  • 13. INCREMENT AND DECREMENT  Prefix:  ++x  x is replaced by x+1, and the value is x+1  --x  x is replaced by x-1, and the value is x-1  Postfix:  x++  x is replaced by x+1, but the value is x  x--  x is replaced by x-1, but the value is x
  • 14. OBJECT-ORIENTED CONCEPT (C++) • Objects of the program interact by sending messages to each other
  • 15. BASIC C++  Inherit all C syntax  Primitive data types  Supported data types: int, long, short, float, double, char, bool, and enum  The size of data types is platform-dependent  Basic expression syntax  Defining the usual arithmetic and logical operations such as +, -, /, %, *, &&, !, and ||  Defining bit-wise operations, such as &, |, and ~  Basic statement syntax  If-else, for, while, and do-while
  • 16. BASIC C++ (CONT)  Add a new comment mark  // For 1 line comment  /*… */ for a group of line comment  New data type  Reference data type “&”. Much likes pointer int ix; /* ix is "real" variable */ int & rx = ix; /* rx is "alias" for ix */ ix = 1; /* also rx == 1 */ rx = 2; /* also ix == 2 */  const support for constant declaration, just likes C
  • 17. CLASS DEFINITIONS  A C++ class consists of data members and methods (member functions). class IntCell { public: explicit IntCell( int initialValue = 0 ) : storedValue( initialValue ) {} int read( ) const { return storedValue;} void write( int x ) { storedValue = x; } private: int storedValue; } Avoid implicit type conversion Initializer list: used to initialize the data members directly. Member functions Indicates that the member’s invocation does not change any of the data members. Data member(s)
  • 18. INFORMATION HIDING IN C++  Two labels: public and private  Determine visibility of class members  A member that is public may be accessed by any method in any class  A member that is private may only be accessed by methods in its class  Information hiding  Data members are declared private, thus restricting access to internal details of the class  Methods intended for general use are made public
  • 19. CONSTRUCTORS  A constructor is a special method that describes how an instance of the class (called object) is constructed  Whenever an instance of the class is created, its constructor is called.  C++ provides a default constructor for each class, which is a constructor with no parameters. But, one can define multiple constructors for the same class, and may even redefine the default constructor
  • 20. DESTRUCTOR  A destructor is called when an object is deleted either implicitly, or explicitly (using the delete operation)  The destructor is called whenever an object goes out of scope or is subjected to a delete.  Typically, the destructor is used to free up any resources that were allocated during the use of the object  C++ provides a default destructor for each class  The default simply applies the destructor on each data member. But we can redefine the destructor of a class. A C++ class can have only one destructor.  One can redefine the destructor of a class.  A C++ class can have only one destructor
  • 21. CONSTRUCTOR AND DESTRUCTOR class Point { private : int _x, _y; public: Point() { _x = _y = 0; } Point(const int x, const int y); Point(const Point &from); ~Point() {void} void setX(const int val); void setY(const int val); int getX() { return _x; } int getY() { return _y; } };
  • 22. CONSTRUCTOR AND DESTRUCTOR Point::Point(const int x, const int y) : _x(x), _y(y) { } Point::Point(const Point &from) { _x = from._x; _y = from._y; } Point::~Point(void) { /* nothing to do */ }
  • 23. C++ OPERATOR OVERLOADING class Complex { ... public: ... Complex operator +(const Complex &op) { double real = _real + op._real, imag = _imag + op._imag; return(Complex(real, imag)); } ... }; In this case, we have made operator + a member of class Complex. An expression of the form c = a + b; is translated into a method call c = a.operator +(a, b);
  • 24. OPERATOR OVERLOADING  The overloaded operator may not be a member of a class: It can rather defined outside the class as a normal overloaded function. For example, we could define operator + in this way: class Complex { ... public: ... double real() { return _real; } double imag() { return _imag; } // No need to define operator here! }; Complex operator +(Complex &op1, Complex &op2) { double real = op1.real() + op2.real(), imag = op1.imag() + op2.imag(); return(Complex(real, imag)); }
  • 25. FRIEND  We can define functions or classes to be friends of a class to allow them direct access to its private data members class Complex { ... public: ... friend Complex operator +( const Complex &, const Complex & ); }; Complex operator +(const Complex &op1, const Complex &op2) { double real = op1._real + op2._real, imag = op1._imag + op2._imag; return(Complex(real, imag)); }
  • 26. STANDARD INPUT/OUTPUT STREAMS  Stream is a sequence of characters  Working with cin and cout  Streams convert internal representations to character streams  >> input operator (extractor)  << output operator (inserter)