classes_and_objects.ppt

13
Classes,objects and memory When a class is declared memory is not allocated to data members of a class. Data members cannot be manipulated unless an object is created for the class. When an object is created, memory is allocated only to its data members not to member functions. Member functions are created and stored in memory only once when a class specification is declared.

Upload: sujithmv

Post on 28-Sep-2015

227 views

Category:

Documents


5 download

TRANSCRIPT

  • Classes,objects and memoryWhen a class is declared memory is not allocated to data members of a class.Data members cannot be manipulated unless an object is created for the class.When an object is created, memory is allocated only to its data members not to member functions.Member functions are created and stored in memory only once when a class specification is declared.

  • Class and Object DiagramsRelationship between a class and it objects using the Unified Modeling Language UML diagram

    object

    bankAccount one

    bankAccount

    class

    interface

    state

    my_name

    my_balance

    withdraw

    deposit

    balance

    name

    object

    bankAccount two

    my_name = "Daly"

    my_balance = 75.00

    my_name = "Stoers"

    my_balance = 333.33

  • Arrays with in a classA Class can have an array member as its member variable.It either can be private/public/ protected data member of a class

  • Array of objectsC++ supports arrays of any data type.An array having class type elements is known as array of objects.It is used to handle a group of objects which reside continuously in memory.Only memory space for data members of the objects is created. Member functions are stored separately and shared by all objects of the class The individual element of an array of objects is referenced by using its index and member of an object is accessed using the dot operatorsyntax:Arrayname[index].membername

  • Objects as function ArgumentsAn object can be passed as a function argument.An object can be passed by Value: A copy of entire object is passed to the functionReference: The address of the object is passed implicitly to the functionPointer : The address of the object is passed explicitly to the function.

  • Returning objectsSimilar to returning variables from functions, it is also possible to return objects from functions.The return type of the function is declared as the return object type.

  • Static variablesAre like global variables for its class.A static member is globally available for all objects of that class type.Usually used to store values common to the entire class.Since they are associated with the class itself, rather than any object also known as class variables.They are stored individually rather than as an element of an object. Only one copy of static member is shared by all objects of a class.

  • Static member variablesTo make a data member static two things are requiredDeclaration within the class definitionDefinition outside the class definitionDeclaration within class is similar to any other variable declarationstatic type varname;Definition outside the class is as follows:type classname:: varname=initial value

  • Static member functionsA static function can access only static members of the same class.Use keyword static before the function declaration.It can be invoked by using the class name instead of object name.Also possible to invoke static member functions using objects.

  • This PointerHow do member functions know which objects data to manipulate???A pointer called this pointer is implicitly defined in each member function and points to the object with which the member function is associated.When a member is invoked, it comes into existence with the value of this set to the address of the object for which it is called.It is not available in static member functions.this pointer can be used to access data in the object.

  • Constant Member FunctionsA constant member function can access a data member without modifying it.Keyword const is used to specify a constant member function.It is used to indicate that the function does not alter the data fields of the object but only inspects them.Syntax : type function name(arg list) const

  • 2-D ArraysMost scientific data can be modeled using multi-dimensional arrays.Format of a 2-D array is type array_name[r_size][c_size];

  • Structures and EncapsulationStructures can have two types of members: data members and member functions.The only difference between a structure and a class is that, by default, the members of a class are private while, by default, members of a structure are public.