- Programming Paradigms
* Procedural Programming Top-Down Approach
* Object-Based Programming - Classes, Objects, Encapsulation,Operator Overloading BUT NO INHERITANCE e.g. Visual Basic
* Object-Oriented Programming - Above + Inheritance, Polymorphism e.g. VC++
* Generic Programming - Function Templates, Class Templates
* Another Aspect - event-driven programming, graphics programming, GUI & multimedia programming
- Introduction to C++
* Developed by Bjarne Stroustrup at Bell Labs
* C++ = C with Classes
- Difference between C & C++
* C is procedural and does not support object-oriented features (PIE)
* C does not have call-by-reference
* C does not have single line comment
* C++ does not allow omission of function return-vale type
* C does not have new and delete
* C++ has better I/O.. cout, cin, stream insertion extraction operators can be
used for own data-types (operator overloading)
* In C++, variables can be declared anywhere before their use
* C++ is enhanced version of C. So, C++ compiler will compile C program but not vice-versa.
- Advantages of OOPs
* Complexity Management
Through Abstraction, Encapsulation, and Information Hiding. No Global Variables.
* Software Reuse (of classes/functions)
Productivity Increases: Development time is reduced as no need to define own classes.
Quality Increases: Re-usable software are efficiently & well designed, developed,tested and maintained.
- Library and Header Files
* Each library has a corresponding header file
* Header file contains function prototypes, data-types and constants
Old Style: New Style: "iostream"
* Inline Functions
Increased Program Size Function code is substituted wherever inline function is called
Reduced Execution Time No overhead of function call
Use only for small and frequently used functions
Change in inline function requires all clients to be re-compiled.
* Convention
- std::cout
- using std::cout
- using namespace std
* Keywords: All of C + others
* Passing Arguments to Functions
Call-By-Value: Use for small and non-modifiable arguments
Call-By-Reference:
- Increased Performance =¿ No overhead of coping large amount of data
- Weaker Security =¿ Caller's data can be corrupted
(a) Using Pointers
(b) using Reference Parameters
- Alias (another name) for corresponding argument
- Pass large data by const reference =¿ Performance + Security
- Prototype: void SqByRef(int &) Call: SqByRef(z) Defn: void SqByRef(int &a)a*=a;
- Reference is obtained by pre-pending the variable name with &
_ By looking at calling, you cannot tell if variable is passed by value or by reference i.e. whether argument will be modified or not.
* More on Reference
- References can also be used as alias for other variables within a function
- References must be initialized in their declarations. Not doing so is a syntax error
int count = 1;
int &cref = count;
- Neither taking the address of a reference nor comparing references cause syntax
errors => Each operation actually occurs on the variable for which reference is an alias.
- A reference argument must be an lvalue and not a constant or expression that returns an rvalue.
int &x=a,y=b,z=c;
int &x,y,z; //wrong
++cref;
- Returning a pointer or reference to an automatic variable in a called function is a logical error i.e.compiler may WARN. References to undefined variables are called Dangling References.
* Empty parameter list disables checking. C not equal to C++
* Default Arguments
- Simple but not clean approach
- If ommitted then auto-inserted by compiler & and passed in call.
- Must be rightmost (trailing) arguments in function parameter list
- If XXX then others should also be omitted
* Unary Scope Resolution Operator
Used for local and global variables with the same name
* Parameterized Stream Manipulator
#include
setprecision(20)-- after decimal places
setw(28)
setiosflag (ios::fixed|ios::showpoint) - Inclusive OR
* Function Overloading:
Used to create several functions of the same name that perform similar tasks on different data types. Should differ in numbers, types and orderof arguments in call. Different only in return types is not function overloading - It issyntax error Operator Overloading/Function Templates = auto generating overloading functions that perform identical tasks on different data types signatures (function name+parameter type)-name mangling or name decoration to enable type-safe linkage- ensures that the proper overloaded function is called and the arguments conform toparameters. Linkage errors are detected and reported by the compiler. Overloadedfunctions with default parameters may cause ambiguity.
* Function Templates
Enable creation of functions that perform the same (identical)
operations on different types of data but the function template is defined only once.
C++ automatically generates separate template function to handle each type of call
appropriately.
template
T maximum(T i, T j)
{
int max;
i>j?max=i:max=j;
return max;
}
Formal type parameters are built-in types or user-defined types used to specify the
types of the arguments to the function, to specify return type of function and todeclare variables within the body of the function definition.
* Class = Structure + Function
- Structures: Group data elements
- Functions: Organize program actions into named entities
- Classes: Group data elements + Organize program actions into named entities
* Role of Variable Type
- Size
- Information it can hold
- Supported operations
* Creating New Types
- C++ allows creating new types as powerful as built-in types
- Why new types: Closer rep resentation of real world makes writing programs easier e.g. heating systems have variables like rooms, heat sensors
In next post, we will see in detail classes with respect to data abstraction.
Source: C++ - How To Program by Deitel & Deitel