learn c programming language

27
The C++ Language www.myassignmenthelp.n et

Upload: myassignmenthelpp

Post on 30-Dec-2015

76 views

Category:

Documents


0 download

DESCRIPTION

Learn C Programming Language with www.myassignmenthelp.net - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Learn C   Programming Language

The C++ Language

www.myassignmenthelp.net

Page 2: Learn C   Programming Language

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

Page 3: Learn C   Programming Language

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

Page 4: Learn C   Programming Language

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

Page 5: Learn C   Programming Language

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

Page 6: Learn C   Programming Language

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

Page 7: Learn C   Programming Language

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

Page 8: Learn C   Programming Language

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

Page 9: Learn C   Programming Language

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

Page 10: Learn C   Programming Language

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).

Page 11: Learn C   Programming Language

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'.

Page 12: Learn C   Programming Language

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

Page 13: Learn C   Programming Language

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

Page 14: Learn C   Programming Language

Object-Oriented Concept (C++)

www.myassignmenthelp.net

• Objects of the program interact by sending messages to each other

Page 15: Learn C   Programming Language

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

Page 16: Learn C   Programming Language

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

Page 17: Learn C   Programming Language

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 conversionInitializer list: used to initialize the datamembers directly.

Member functions

Indicates that the member’s invocation doesnot change any of the data members.

Data member(s)

Page 18: Learn C   Programming Language

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

Page 19: Learn C   Programming Language

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

Page 20: Learn C   Programming Language

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

Page 21: Learn C   Programming Language

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

Page 22: Learn C   Programming Language

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

Page 23: Learn C   Programming Language

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

Page 24: Learn C   Programming Language

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

Page 25: Learn C   Programming Language

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

Page 26: Learn C   Programming Language

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

Page 27: Learn C   Programming Language

Thank You

www.myassignmenthelp.net