1 / 27

Learn C Programming Language

Learn C Programming Language with www.myassignmenthelp.net

Download Presentation

Learn C Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The C++ Language www.myassignmenthelp.net

  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 • 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). www.myassignmenthelp.net

  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'. www.myassignmenthelp.net

  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++) • Objects of the program interact by sending messages to each other www.myassignmenthelp.net

  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 www.myassignmenthelp.net

  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. Initializer list: used to initialize the data members directly. Avoid implicit type conversion Member functions Indicates that the member’s invocation does not change any of the data members. Data member(s) 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; } www.myassignmenthelp.net

  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 destructorfor 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 cinand cout • Streams convert internal representations to character streams • >> input operator (extractor) • << output operator (inserter) www.myassignmenthelp.net

  27. Thank You www.myassignmenthelp.net

More Related