SlideShare a Scribd company logo
The C++ Language
www.myassignmenthelp.net
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
www.myassignmenthelp.net
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
www.myassignmenthelp.net
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.
www.myassignmenthelp.net
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;
}
www.myassignmenthelp.net
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.
www.myassignmenthelp.net
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;
www.myassignmenthelp.net
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
www.myassignmenthelp.net
C++ Data Type
www.myassignmenthelp.net
• 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
www.myassignmenthelp.net
• 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
www.myassignmenthelp.net
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
www.myassignmenthelp.net
Object-Oriented Concept (C++)
www.myassignmenthelp.net
• Objects of the program interact by sending messages to each other
Basic C++
www.myassignmenthelp.net
• 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
www.myassignmenthelp.net
Class Definitions
www.myassignmenthelp.net
• 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
www.myassignmenthelp.net
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
www.myassignmenthelp.net
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
www.myassignmenthelp.net
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; }
};
www.myassignmenthelp.net
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 */
}
www.myassignmenthelp.net
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);
www.myassignmenthelp.net
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));
}
www.myassignmenthelp.net
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));
}
www.myassignmenthelp.net
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)
www.myassignmenthelp.net
Thank You
www.myassignmenthelp.net

More Related Content

PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPT
Introduction To C#
SAMIR BHOGAYTA
 
PPT
Introduction to C++
Bharat Kalia
 
PPTX
Function C++
Shahzad Afridi
 
PPTX
C++ presentation
SudhanshuVijay3
 
PPTX
Object Oriented Programming in Java _lecture 1
Mahmoud Alfarra
 
PPTX
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 
Introduction To C#
SAMIR BHOGAYTA
 
Introduction to C++
Bharat Kalia
 
Function C++
Shahzad Afridi
 
C++ presentation
SudhanshuVijay3
 
Object Oriented Programming in Java _lecture 1
Mahmoud Alfarra
 
Object Oriented Programming using C++(UNIT 1)
Dr. SURBHI SAROHA
 

What's hot (20)

PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
PPT
Programming in c
indra Kishor
 
PPT
Introduction to c programming
ABHISHEK fulwadhwa
 
PPT
Constructor and destructor in C++
Carelon Global Solutions
 
PPSX
Complete C++ programming Language Course
Vivek Singh Chandel
 
PPT
Compiler Design
Mir Majid
 
PPT
Input and output in C++
Nilesh Dalvi
 
PPTX
Conditional statement c++
amber chaudary
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PDF
How to Work with Dev-C++
Deepak Jha
 
PPTX
Introduction to c programming
Manoj Tyagi
 
PDF
Function in C
Dr. Abhineet Anand
 
PPT
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
PPTX
Object oriented programming in java
Elizabeth alexander
 
PPTX
C++ language basic
Waqar Younis
 
PPTX
Abstract class and Interface
Haris Bin Zahid
 
PPT
OOP in C++
ppd1961
 
FUNCTIONS IN c++ PPT
03062679929
 
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Programming in c
indra Kishor
 
Introduction to c programming
ABHISHEK fulwadhwa
 
Constructor and destructor in C++
Carelon Global Solutions
 
Complete C++ programming Language Course
Vivek Singh Chandel
 
Compiler Design
Mir Majid
 
Input and output in C++
Nilesh Dalvi
 
Conditional statement c++
amber chaudary
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Exception Handling in C#
Abid Kohistani
 
How to Work with Dev-C++
Deepak Jha
 
Introduction to c programming
Manoj Tyagi
 
Function in C
Dr. Abhineet Anand
 
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
Object oriented programming in java
Elizabeth alexander
 
C++ language basic
Waqar Younis
 
Abstract class and Interface
Haris Bin Zahid
 
OOP in C++
ppd1961
 
Ad

Viewers also liked (20)

PPT
Basics of c++ Programming Language
Ahmad Idrees
 
PDF
Intro to C++ - language
Jussi Pohjolainen
 
PDF
C++ Programming : Learn OOP in C++
richards9696
 
PPTX
Overview- Skillwise Consulting
Skillwise Group
 
PPSX
Support for Object-Oriented Programming (OOP) in C++
Ameen Sha'arawi
 
PPTX
Innovative web design in delhi
Jatin Arora
 
PPT
The smartpath information systems c plus plus
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
PPTX
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
PPT
C++ OOP Implementation
Fridz Felisco
 
PPTX
Control structures in c++
Nitin Jawla
 
PPTX
Pipe & its wall thickness calculation
sandeepkrish2712
 
PDF
Valve selections
Sandip Sonawane
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PPS
basics of C and c++ by eteaching
eteaching
 
PPT
CBSE Class XI Programming in C++
Pranav Ghildiyal
 
PDF
Multidimensional arrays in C++
Ilio Catallo
 
PPTX
Cs1123 3 c++ overview
TAlha MAlik
 
PPT
Overview of c++
geeeeeet
 
PPTX
C++ Overview PPT
Thooyavan Venkatachalam
 
Basics of c++ Programming Language
Ahmad Idrees
 
Intro to C++ - language
Jussi Pohjolainen
 
C++ Programming : Learn OOP in C++
richards9696
 
Overview- Skillwise Consulting
Skillwise Group
 
Support for Object-Oriented Programming (OOP) in C++
Ameen Sha'arawi
 
Innovative web design in delhi
Jatin Arora
 
The smartpath information systems c plus plus
The Smartpath Information Systems,Bhilai,Durg,Chhattisgarh.
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
C++ OOP Implementation
Fridz Felisco
 
Control structures in c++
Nitin Jawla
 
Pipe & its wall thickness calculation
sandeepkrish2712
 
Valve selections
Sandip Sonawane
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
basics of C and c++ by eteaching
eteaching
 
CBSE Class XI Programming in C++
Pranav Ghildiyal
 
Multidimensional arrays in C++
Ilio Catallo
 
Cs1123 3 c++ overview
TAlha MAlik
 
Overview of c++
geeeeeet
 
C++ Overview PPT
Thooyavan Venkatachalam
 
Ad

Similar to Learn c++ Programming Language (20)

PPTX
c++.pptx ppt representation of c plus lanjhgvghihh
KeshavMotivation
 
PPTX
C++ language
Hamza Asif
 
PPTX
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
PPTX
C programming language tutorial
Dr. SURBHI SAROHA
 
PPTX
Cpp-c++.pptx
LightroomApk
 
PPTX
Presentation on topic of c and c++ programming language.(.pptx
panawarahul7
 
PPTX
Net framework
Abhishek Mukherjee
 
PPTX
C
Jerin John
 
PPTX
Programming Language
Adeel Hamid
 
PPT
C++ - A powerful and system level language
dhimananshu130803
 
PPT
lecture02-cpp.ppt
nilesh405711
 
PPT
lecture02-cpp.ppt
YashpalYadav46
 
PPT
lecture02-cpp.ppt
ssuser0c24d5
 
PPT
lecture02-cpp.ppt
DevliNeeraj
 
PPTX
C_plus_plus
Ralph Weber
 
PPTX
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
PPT
lecture5-cpp.pptintroduccionaC++basicoye
quetsqrj
 
PPT
Introduction to Inheritance in C plus plus
University of Sindh
 
c++.pptx ppt representation of c plus lanjhgvghihh
KeshavMotivation
 
C++ language
Hamza Asif
 
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
C programming language tutorial
Dr. SURBHI SAROHA
 
Cpp-c++.pptx
LightroomApk
 
Presentation on topic of c and c++ programming language.(.pptx
panawarahul7
 
Net framework
Abhishek Mukherjee
 
Programming Language
Adeel Hamid
 
C++ - A powerful and system level language
dhimananshu130803
 
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
YashpalYadav46
 
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
DevliNeeraj
 
C_plus_plus
Ralph Weber
 
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
NagarathnaRajur2
 
lecture5-cpp.pptintroduccionaC++basicoye
quetsqrj
 
Introduction to Inheritance in C plus plus
University of Sindh
 

More from Steve Johnson (14)

PPTX
Science of boredom
Steve Johnson
 
PPTX
How to create effective powerpoint presentation
Steve Johnson
 
PPTX
How to approach your first assignment
Steve Johnson
 
PPTX
Algorithm
Steve Johnson
 
PPTX
A quick guide on accounting process of bookkeeping
Steve Johnson
 
PPTX
Online education vs classroom teaching
Steve Johnson
 
PPTX
Darwin’s theory of evolution
Steve Johnson
 
PPT
Learn Vba excel 2007
Steve Johnson
 
ODP
Learn Data Structures With Myassignmenthelp.Net
Steve Johnson
 
PPTX
What is a Scientific Presentation ?
Steve Johnson
 
PPTX
Difference Between Sql - MySql and Oracle
Steve Johnson
 
PPTX
What is Psychology ?
Steve Johnson
 
PPT
Biology Assignments
Steve Johnson
 
PPT
Biology Assignments
Steve Johnson
 
Science of boredom
Steve Johnson
 
How to create effective powerpoint presentation
Steve Johnson
 
How to approach your first assignment
Steve Johnson
 
Algorithm
Steve Johnson
 
A quick guide on accounting process of bookkeeping
Steve Johnson
 
Online education vs classroom teaching
Steve Johnson
 
Darwin’s theory of evolution
Steve Johnson
 
Learn Vba excel 2007
Steve Johnson
 
Learn Data Structures With Myassignmenthelp.Net
Steve Johnson
 
What is a Scientific Presentation ?
Steve Johnson
 
Difference Between Sql - MySql and Oracle
Steve Johnson
 
What is Psychology ?
Steve Johnson
 
Biology Assignments
Steve Johnson
 
Biology Assignments
Steve Johnson
 

Recently uploaded (20)

PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Software Development Company | KodekX
KodekX
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Software Development Methodologies in 2025
KodekX
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 

Learn c++ Programming Language

  • 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 www.myassignmenthelp.net
  • 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 www.myassignmenthelp.net
  • 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. www.myassignmenthelp.net
  • 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; } www.myassignmenthelp.net
  • 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. www.myassignmenthelp.net
  • 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; www.myassignmenthelp.net
  • 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 www.myassignmenthelp.net
  • 10. C++ Data Type www.myassignmenthelp.net • 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 www.myassignmenthelp.net • 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 www.myassignmenthelp.net
  • 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 www.myassignmenthelp.net
  • 14. Object-Oriented Concept (C++) www.myassignmenthelp.net • Objects of the program interact by sending messages to each other
  • 15. Basic C++ www.myassignmenthelp.net • 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 www.myassignmenthelp.net
  • 17. Class Definitions www.myassignmenthelp.net • 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 www.myassignmenthelp.net
  • 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 www.myassignmenthelp.net
  • 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 www.myassignmenthelp.net
  • 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; } }; www.myassignmenthelp.net
  • 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 */ } www.myassignmenthelp.net
  • 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); www.myassignmenthelp.net
  • 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)); } www.myassignmenthelp.net
  • 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)); } www.myassignmenthelp.net
  • 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) www.myassignmenthelp.net