You are on page 1of 4

Programming Paradigms Procedural Programming Top-Down Approach Object-Based Programming - Classes, Objects, Encapsulation,Operator Overloading Object-Oriented Programming - Above

+ Inheritance, Polymorphism Generic Programming - Function Templates, Class Templates Another Aspect - event-driven programming, graphics programming, GUI & multimedia programming Object-Oriented Terminology Class Object Encapsulation: Bundling together all information, capabilities and responsibilities of an entity into a single object Adv: Only need to know what a class does and not how it does it Data-Hiding: Data is concealed within the class so that it cannot be accessed mistakenly by the functions outside class. Inheritance: Polymorphism: 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 Advantages of OOPs Complexity Management Through Abstraction, Encapsulation, and Information Hiding. No Global Variables. Software Reuse (of classes/functions) 1 _ 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. Prepared by: Syed Feroz Zainvi E-mail: zainvi.sf@gmail.com

C++ is enhanced version of C. So, C++ compiler will compile C program but not viceversa.

Available at: http://www.zainvi.tophonors.com Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

Library and Header Files Each library has a corresponding header file Header file contains function prototypes, data-types and constants \textit{Old Style}: <iostream.h> \textit{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 1. Call-By-Value: Use for small and non-modifiable arguments 2. Call-By-Reference: Increased Performance = No overhead of coping large amount of data Weaker Security = Callers 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 2 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; $spadesuite$ int &x,y,z; \$wrong$/ Prepared by: Syed Feroz Zainvi E-mail: zainvi.sf@gmail.com Available at: http://www.zainvi.tophonors.com Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

++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 <iomanip> 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 order of arguments in call. Different only in return types is not function overloading It is syntax 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 typesafe linkage - ensures that the proper overloaded function is called and the arguments conform to parameters. Linkage errors are detected and reported by the compiler. Overloaded functions 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. 3 template <class T> T maximum(T i, T j) { int max; Prepared by: Syed Feroz Zainvi E-mail: zainvi.sf@gmail.com Available at: http://www.zainvi.tophonors.com Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

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 to declare 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 4

Prepared by: Syed Feroz Zainvi E-mail: zainvi.sf@gmail.com

Available at: http://www.zainvi.tophonors.com Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

You might also like