Documente Academic
Documente Profesional
Documente Cultură
www.myassignmenthelp.n
et
Overview of C++
www.myassignmenthelp.
Outline
C++ basic features
Programming paradigm and statement syntax
Class definitions
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.
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.
www.myassignmenthelp.
www.myassignmenthelp.
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.
std::cin;
std::cout;
std::string;
std::getline;
www.myassignmenthelp.
www.myassignmenthelp.
www.myassignmenthelp.
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'.
www.myassignmenthelp.
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.
--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
www.myassignmenthelp.
Basic C++
www.myassignmenthelp.
www.myassignmenthelp.
Class Definitions
class IntCell
{
public:
explicit IntCell( int initialValue = 0 )
: storedValue( initialValue ) {}
Member
functions
www.myassignmenthelp.
www.myassignmenthelp.
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.
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
www.myassignmenthelp.
val);
val);
_x; }
_y; }
};
www.myassignmenthelp.
: _x(x), _y(y) {
www.myassignmenthelp.
www.myassignmenthelp.
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.
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.
www.myassignmenthelp.
Thank You
www.myassignmenthel
p.net