cas better cpp

Upload: hussain-hadi

Post on 04-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Cas Better Cpp

    1/4

    Programming ParadigmsProcedural Programming Top-Down ApproachObject-Based Programming - Classes, Objects, Encapsulation,OperatorOverloadingObject-Oriented Programming - Above + Inheritance, Polymorphism

    Generic Programming - Function Templates, Class TemplatesAnother Aspect - event-driven programming, graphics programming, GUI &multimediaprogramming Object-Oriented TerminologyClassObjectEncapsulation: Bundling together all information, capabilities andresponsibilities ofan entity into a single objectAdv: 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 accessedmistakenlyby 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 beused for own data-types (operator overloading) In C++, variables can be declared anywhere before their useC++ is enhanced version of C. So, C++ compiler will compile C program but not vice-versa. Advantages of OOPs Complexity ManagementThrough Abstraction, Encapsulation, and Information Hiding. No Global

    Variables. Software Reuse (of classes/functions)1_ Productivity Increases: Development time is reduced as no need to defineown classes._ Quality Increases: Re-usable software are efficiently & well designed,developed,tested and maintained.

    Prepared by: Syed Feroz Zainvi Available at: http://www.zainvi.tophonors.com

    E-mail:[email protected] Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

    http://www.zainvi.tophonors.com/mailto:[email protected]://www.zainvi.tophonors.com/mailto:[email protected]
  • 7/30/2019 Cas Better Cpp

    2/4

    Library and Header Files Each library has a corresponding header file Header file contains function prototypes, data-types and constants \textit{Old Style}: \textit{New Style}: "iostream" Inline Functions

    Increased Program Size Function code is substituted wherever inline function iscalledReduced Execution Time No overhead of function callUse only for small and frequently used functionsChange 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 arguments2. 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 orby 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 function2 References must be initialized in their declarations. Not doing so is a syntaxerrorint count = 1;int &cref = count; Neither taking the address of a reference nor comparing references causesyntax

    errors = Each operation actually occurs on the variable for which reference isanalias. A reference argument must be an lvalue and not a constant or expressionthatreturns an rvalue.int \&x=a,y=b,z=c; $spadesuite$int &x,y,z; \$wrong$/

    Prepared by: Syed Feroz Zainvi Available at: http://www.zainvi.tophonors.com

    E-mail:[email protected] Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

  • 7/30/2019 Cas Better Cpp

    3/4

    ++cref; Returning a pointer or reference to an automatic variable in a called functionisa logical error i.e. compiler may WARN. References to undefined variables arecalled 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 OperatorUsed 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 thatperformsimilar 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 generatingoverloadingfunctions 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 argumentsconform 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 onlyonce.

    C++ automatically generates separate template function to handle each typeof callappropriately.3template T maximum(T i, T j){int max;

    Prepared by: Syed Feroz Zainvi Available at: http://www.zainvi.tophonors.com

    E-mail:[email protected] Source: Ajax: The Definitive Guide, Anthony T. Holdener, III

  • 7/30/2019 Cas Better Cpp

    4/4

    i>j?max=i:max=j;return max;}Formal type parameters are built-in types or user-defined types used to specifythe

    types of the arguments to the function, to specify return type of function andtodeclare 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 namedentities 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 programseasier e.g. heating systems have variables like rooms, heat sensors4

    Prepared by: Syed Feroz Zainvi Available at: http://www.zainvi.tophonors.com

    E-mail:[email protected] Source: Ajax: The Definitive Guide, Anthony T. Holdener, III