Key Unit Competence:

    To be able to explain common concepts of Object Oriented Programming (OOP) and
    implement them in C++.


    Human beings can be grouped into males and females. Each of them has two legs, two
    hands, two eyes, a nose, a heart, and so on. There are body parts that are common to
    males and females, but there are specific body parts for males and not for Females, and
    vice versa.
    Both Males and Females perform some common functions like walking, eating, seeing,
    talking, hearing etc. But there are some specific functions for each group of Human

    After reading the above, do the following.

    1. Categorize human beings into two main groups

    2. What are the body parts that all human beings have in common?

    3. List the parts that are specific for females and males.

    4. What all human beings can do in common? What are specific functions
         only for each gender?

    5. Based on database concepts, write down the properties of the entities
       human being, male and female. Design on papers a Diagram of entities
       found in the above scenario.

    6. Add to each entity the functions raised in question 3, 4 and 5. Which
        entity depends on the other one?

    7. Do you need to repeat the properties and functions of human being
        entity to male entity and to female entity? Explain your answer.

    8. After considering the question above, design a diagram of the new

    9. Discuss your answer

    7.1. Introduction to Object Oriented Programming (OOP)

    Activity 7.1

    Compare the programming paradigms that you have learnt so far.

    1. Which one can help you to create a program for the “Introductory

    2. Search and write an essay on the concepts and principles of that new
        programming paradigm.

    7.1.1 Definition of Object Oriented Programming(oop)

    Object-oriented programming (OOP) refers to a type of computer programming (software
    design) in which programmers define not only the data type of a data structure, but also
    the types of operations (functions) that can be applied to the data structure.
    In this way, the data structure becomes an object that includes both data and functions. In
    addition, programmers can create relationships between one object and another.

    7.1.2 Basic Concepts of Object Oriented Programming

    The main concepts and principles used within Object Oriented Programming are:

    • Objects

    • Classes

    • Abstraction

    • Encapsulation

    • Inheritance

    • Polymorphism

    • Exception Handling

    Definitions of OOP concepts

                    a. Object

    An objects is the basic unit of OOP. It is an instance of class, which has data members and
    use various member functions to perform tasks.

                   b. Class

    A class is basically a blueprint of an object. It can also be defined as user defined data type
    but it also contains functions in it. It declares and defines what data variables the object will
    have and what operations can be performed on the class’s object.

                    c. Encapsulation

    Encapsulation means that the internal representation of an object is generally hidden from
    view outside of the object’s definition. Typically, only the object’s own methods can directly
    inspect or manipulate its fields. It can also be said data binding. Encapsulation is all about
    binding the data variables and functions together in class.

                   d. Abstraction

    Data abstraction and encapsulation are closely tied together, because a simple definition
    of data abstraction is the development of classes, objects, types in terms of their interfaces
    and functionality, instead of their implementation details. Abstraction denotes a model, a
    view, or some other focused representation for an actual item.
    In C++, classes can provide methods to the outside world to access & use the data variables,
    keeping the variables hidden from direct access, or classes can even declare everything
    accessible to everyone, or maybe just to the classes inheriting it. This can be done using
    access specifiers.

                e. Inheritance

    Inheritance as the key feature of Object-Oriented Programming is a way to reuse code
    of existing objects, or to establish a subtype from an existing object, or both, depending
    upon programming language support. In classical inheritance where objects are defined
    by classes, classes can inherit attributes and behavior from pre-existing classes called base
    classes, super classes, parent classes or ancestor classes. The resulting classes are known as
    derived classes, subclasses or child classes. The relationships of classes through inheritance
    gives rise to a hierarchy.
    The derived class inherits all the features from the base class and can have additional
    features of its own.

                 f. Polymorphism

    It is a feature, which lets programmers create functions with same name but different
    arguments, which will perform different actions. That means, functions with same name, but
    functioning in different ways. Or, it also allows users to redefine a function and provide it with
    a completely new definition.

                g. Exception Handling

    Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced
    at runtime (during the running of the program).

    Graphically the principles of Object Oriented Paradigm can be represented like this.

    Advantages of Object Oriented Programming

    One of the principal advantages of Object-Oriented Programming techniques over procedural
    programming techniques is that they enable programmers to create modules that do
    not need to be changed when a new type of object is added. A programmer can simply
    create a new object that inherits many of its features from existing objects. This makes
    object-oriented programs easier to modify.
    So, Object Oriented Programming has great advantages over other programming styles.

    1. Code Reuse and Recycling: Objects created for Object Oriented
       Programming can easily be reused in other programs.

    2. Data Redundancy: Inheritance is the good feature for data redundancy. If
        you need a same functionality in multiple classes, you can write a common
        class for the same functionality and inherit that class to sub class.

    3. Data hiding: Implementation details are hidden from other modules and
        other modules has a clearly defined interface.

    4. Design Benefits: Large programs are very difficult to write. Object
       Oriented Programming force designers to go through an extensive
       planning phase, which makes for better designs with less flaws. In
      addition, once a program reaches a certain size, Object Oriented Programs
      are actually easier to program than non-Object Oriented ones.

    5. Software Maintenance: An Object Oriented Program is much easier to
       modify and maintain than a non-Object Oriented Program. So although a
      lot of work is spent before the program is written, less work is needed to
     maintain it over time.

    Application Activity 7.1

    1. The following are the principles of OOP: Object, Class, Encapsulation,
        inheritance and Polymorphism. Explain them one by one.

    2. What are the advantages of OOP?

    3. Name the features that are added to standard C++.

    7.2 Class definition in C++

    Activity 7.2

    Observe the C++ program below and answer the following questions:


    Using namespace std;

    class Test


    int a;

    float b;


    void number1()

    { a = 2;

    return a;}

    float number2()

    b = 3.5;
    return b;

    int main()

    test m1;

    cout <<”First number is: “<< m1.number1()<<’\n’;

    cout <<”Second number is: “<< m1.number2()<<’\n’;



    From the program above, answer the following questions:

    1. What are new key words in the above program?

    2. Describe the structure of the part starting with the word class.

    3. List the data members contained in the class “Test”

    4. What are the functions found in that class?

    5. What is the role of the variable m1?

    7.2.1 Definition of a class

    A class is defined in C++ using keyword class followed by the name of class.
    The body of class is defined inside the curly brackets and terminated by a semicolon at the

    7.2.2 Syntax

    Here, the class is named Test. This class has two data members: data_1 and data_2 and two
    member functions: function_1() and function_2().
    A Class is an expanded concept of data structures, like data structures, it can contain data
    members, but it can also contain functions as members. Where class_name is a valid identifier
    for the class, object_names is an optional list of names for objects of this class. The body
    of the declaration can contain members, which can either be data or function declarations,
    and optionally access specifiers.

    Classes have the same format as plain data structures, except that they can also include
    functions and have these new things called access specifiers (data hiding). An access specifier
    is one of the following three keywords: private, public or protected.
    Access specifiers are used to identify access rights for the data and member functions of the
    class. There are three main types of access specifiers in C++ programming language:

    A private member within a class denotes that only members of the same class
      have accessibility. The private member is inaccessible from outside the class.

    Public members are accessible from outside the class.

    A protected access specifier is a stage between private and public access. If
    member functions defined in a class are protected, they cannot be accessed
    from outside the class but can be accessed from the derived class.
    When defining access specifiers, the programmer must use the keywords: private, public or
    protected when needed, followed by a colon and then define the data and member functions
    under it.

    class AddNumbers

    int x,y;
    void sum()

    In the code above, the member x and y are defined as private access specifiers. The member
    function sum is defined as a public access specifier.
    General structure for defining a class is:

    Generally, in class, all members (data) would be declared as private and the member functions
    would be declared as public. If no access specifiers are identified for members of a
    class, the members are defaulted to private access (Private is the default access level for

    class AddNumbers
    int a,b;


    void sum()

    In this example, for members a and b of the class AddNumbers there are no access specifiers
    identified. This means that by default a and b are private members of class AddNumbers.
    You can have functions inside a class. These functions can be either under public or private,
    and their syntaxes are the same. For example, if you have a function to display information
    of an account called display, and other classes should be able to access that function, the
    following line would be added under the public area:

    void display (accountno);
    And use the following syntax to implement the function:
    void BankAccount::display (int accountno)

    The computer accesses an object through the use of one of the object’s methods. The
    method performs some action to the data in the object and returns this value to the

    computer. Classes of objects can also be further grouped into hierarchies, in which objects
    of one class can inherit methods from another class. The structure provided in objectoriented
    languages makes them very useful for complicated programming tasks.
    7.2.3 Defining member functions inside or outside the class definition
    Member functions of a class can be defined either inside or outside the class definition.
    In both cases, the function body remains the same, however, the function header is

    7.2.3 .I Inside the Class

    A member function of a class can be defined inside the class. However, when a member
    function is defined inside the class, the class name and the scope resolution operator are
    not specified in the function header. Moreover, the member functions defined inside a class
    definition are by default inline functions.
    To understand the concept of defining a member function inside a class, consider this example.

    In this example, the member function putdata() is defined inside the class book. Hence,
    putdata()is by default an inline function.
    Note that the functions defined outside the class can be explicitly made inline by prefixing
    the keyword inline before the return type of the function in the function header. For example,
    consider the definition of the function getdata().
    inline void student ::getdata ()
    body of the function
    } Outside the Class

    Defining a member function outside a class requires the function declaration (function prototype)
    to be provided inside the class definition. The member function is declared inside
    the class like a normal function. This declaration informs the compiler that the function is a
    member of the class and that it has been defined outside the class. After a member function
    is declared inside the class, it must be defined (outside the class) in the program.

    The definition of member function outside the class differs from normal function definition,
    as the function name in the function header is preceded by the class name and the scope
    resolution operator (: smile. The scope resolution operator informs the compiler what class the
    member belongs to.

    Here is the syntax for defining a member function outside the class:

    Return_type class_name :: function_name (parameter_list)
    // This is the body of the function

    To understand the concept of defining a member function outside a class, consider this


     Let us re-write the program which converts the degree from Fahrenheit to Celsius and displays
     the corresponding degrees on the screen. By creating the method outside the class,
     we have:

    Note that the member functions of the class can access all the data members and other
    member functions of the same class (private, public or protected) directly by using their
    names. In addition, different classes can use the same function name.

    Application Activity 7.2

    The below piece of code is for class declaration, analyze it and answer the following questions:

    7.3 Object in C++

    Activity 7.3

    Your District wants to create a program that will help to know its primary and secondary
    schools. Create the class “school” with the following attributes and functions:
    Decide on the types of attributes

    a. Attributes:

         1. Name of the school

         2. Code of the school

        3. Number of student in your school

        4. Number of teachers

        5. reading the attributes

        6. printing the attributes

    B (i) Write a program in C++ that reads and prints the list of schools available in your

    (ii) complete the table below with the following list:

         1. school

         2. School name

         3. Code of the school

        4. Combinations

        5. Number of students

        6. Number of teachers

         7. Read_school ()

         8. Print_school()

    The first characteristic of an object language is to place at your disposal the objects.
    An object can be regarded as an additional structure of information, a type of super-variable.
    Indeed, we know that a variable is a place in primary memory, characterized by an address
    – a name - and a type (integer, double, character, Boolean, etc).

    In a variable, we can store one and only one information.
    Even if an indexed variable is used - a table - the various memory spaces thus defined will
    store all obligatorily information of the same type.

    7.3.1 Definition:

    An object is an instantiation of a class. In terms of variables, a class would be the type, and
    an object would be the variable. So, an object is a group of variables of various types. It
    thus usually gathers tens of very different information from/to each other within the same
    structure, thus making this information easier to handle. By analogy with equation of Wirth,
    we could say that the equation of the Object Oriented Programming (OOP) is: methods+-

    With the difference of what occurs with a table, the various variables of the same object
    are not indicated by an index, but by a name which is proper for them. In fact, these names
    which characterize the various variables within an object are called properties of the object.
    As consequence, any property of object obeys strictly the rules which apply to the variables
    in any language (type, size, rules of assignment…).
    It will be also said that several objects which have the same properties are of the same type,
    or for better explanation, of the same class.


    let us say that you want to make a program which will record or process the data on the
    books in a library or bookshop, take an object of everyday usage: “A book”.
    The properties of a book are:
    its size (number of the pages), the name of the author, date of publication, printing company,
    its price, its name (title), if a person can borrow it or not, etc.
    You can easily find the type of each one of these properties:

    • The title and the name of author are the properties of the character type.

    • The size (a number of pages) and the price are the properties of the numerical
    The situation “if one can borrow it from the library or not” is a Boolean property.

    7.3.2 Creating single object

    When a class is defined, only the specifications for the object is defined, the memory or
    storage is not allocated. To use the data and access functions defined in the class, you need
    to create objects. Syntax to define Object in C++

    className objectVariableName;
    You can create objects of Test class (defined in above example) as follows:

    Here, two objects o1 and o2 of Test class are created.
    In the above class Test, data1 and data2 are data members and function1() and function2()
    are member functions

    Example: Class and Object

    Number: 12
    Enter data: 23.3
    You entered 23.3
    In this program, two data members data1 and data2 and two memberfunctions insertIntegerData()
    and insertFloatData() are defined under Test class.
    Two objects o1 and o2 of the same class are declared.
    The insertIntegerData() function is called for the o1 object using:
    This sets the value of data1 for object o1 to 12.
    Then, the insertFloatData() function for object o2 is called and the return value from the
    function is stored in variable secondDataOfObject2 using:
    secondDataOfObject2 = o2.insertFloatData();
    In this program, data2 of o1 and data1 of o2 are not used and contains garbage value. Creating array of objects

    7.3.3. Passing object to function

    As it is known, we can pass (give) any type of arguments within the member function which
    can have any numbers of arguments. In C++ programming language, it is also possible to
    pass an object as an argument within the member function of class.
    This is useful, when we want to initialize all data members of an object with another object,
    we can pass objects and assign the values of supplied object to the current object. For complex
    or large projects, we need to use objects as an argument or parameter.

    UNIT 6: POINTERS AND STRUCTURE IN C++UNIT8: Introduction to Visual basic