Question? Leave a message!


GraceRogers Profile Pic
Published Date:12-07-2017
Website URL
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By – Manali Torpe Fundamentals of OOP  Class  Object  Encapsulation  Abstraction  Inheritance  Polymorphism  Reusability C++ as an OOP language  C++ : C with classes  Multi-paradigm language  As Object oriented language, it offers bottom to top approach  As Procedural language, it offers top to bottom approach Classes and objects (I)  Class- user defined data type. Fundamental packaging unit of OOP technology  Class declaration is similar to struct declaration  Keyword ‘class’ followed by class name.  Object is an instance of class  Object combines data and functions  Object is created as a variable of class type using class name  Members of class  Data members / attributes  Member functions / methods Classes and objects (II)  Structure of C++ program with class Data members  Data members can be any of the following types  Primary data types : int, float, char, double, bool  Secondary data types : arrays, pointers, class objects etc.  Data members classified into two groups  Regular : every object gets its own copy of data members  Static: all objects share the same copy of data member Static Data Members  Variable declaration preceded by keyword ‘static’  Only one copy of static variable is created. All the objects share the same copy  Initialized to zero when first object is created. No other initialization permitted.  Should be defined outside the class definition after declaring them inside the class in this way – datatype classname :: varname  They are normally used to maintain values that are common to the entire class, e.g., to keep a count of number of objects created. Methods (I)  Function defined inside a class declaration is called as member function or method  Methods can be defined in two ways - inside the class or outside the class using scope resolution operator (::)  When defined outside class declaration, function needs to be declared inside the class Methods (II) Method defined inside the Method defined outside class the class Methods (III)  Types of functions in a class  Regular functions  Overloaded functions  Inline functions  Friend functions  Static functions  Constructors  Destructors  Virtual functions Inline Function (I)  It is a function defined with a keyword ‘inline’  Compiler replaces the function call with function definition  It can not be recursive  It can not contain any types of loops  It can not have switch cases or nested if’s  It can not have static variable or goto statements  Main() can not be inline Inline Function (II)  All the inline functions must be defined before the call, because compiler needs to go through definition before the call Friend Function (I)  Non-member function  Has access to private and protected data of class. It gets the access through declaration in the class with keyword ‘friend’  It can be declared anywhere in class, i.e., private/public scope  It has minimum one object of the class as its parameter because it accesses data members with the object name  It can not be called by an object, because it is not a member function  One function can be friend of any number of classes. Friend Function (II)  Friend function example Friend function (III)  Uses of Friend function  Useful when overloading certain types of operators  Useful when two or more classes contain members that are interrelated to other parts of program  Enhances encapsulation. Only programmer who has access to the source code of class, can make a function friend of that class Friend Classes  They are used when two or more classes need to work together and need access of each other’s data members without making them accessible by other classes. Static and Const Member Functions  Static member functions-  Can have access to only static members of the same class  Can be called using class name as – classname :: functionname ();  Const member functions-  Function declaration followed by keyword ‘const’, e.g., void put() const statements……..  It ensures that it will never modify any data members  Can be invoked for both const and non-const objects Constructors (I)  Special member function to initialize the objects of its class  Automatically called when an object is created  Data members can be initialized through constructors  Have the same name of the class  They can have any number of parameters  Do not have return types, because they are called automatically by system  A constructor can only be called by a constructor Constructors (II)  Three types of constructors-  Default constructors - constructor with no parameters. Compiler supplies default constructor by itself if not defined explicitly. e.g. Circle() . In main function, Circle c.  Parameterized constructors- constructors with parameters. Used for initializing data members e.g. Circle(float x) r =x; . In main function, Circle c(3.5);  Copy constructors- used when one object of the class initializes other object. It takes reference to an object of the same class as an argument. e.g. Circle (Circle &x) r=x.r; . in main function, Circle c1(3.5); Circle c2=c1; Constructors (III)  Ways of calling the constructors-  Implicit call – Calling the constructor by its object. we do not specify the constructor name (Circle(3.5)) e.g. Circle c(3.5);  Explicit call – constructor is called by its name with parameters E.g. Circle c = Circle(3.5);  Dynamic initialization – first memory is allocated to the object using default constructor. Then parameterized constructor is called to initialize data members E.g. Circle c; float x; cinx; c= Circle(x);