Lecture No.07                       

The basic concept “Object” of Object Orientation (thinking in terms of objects) is realized using classes in programming languages.

01.1.     Class
It is a way (Mechanism) given by c++ to realize objects in a program. It is concrete implementation of objects in c++. We capture any object attributes and behaviour in a programming language using classes.
In other words it can be defined as facility given by c++ to create new types according to our requirement. (Class is composite data type made from basic c++ types like integers, chars and float).

Example:
Consider the examples of entity lion there are many lions but all lions will have similar attributes and behaviour.
Similarly consider student object all students have separate existence but all students have similar attributes and they exhibit similar behaviour.

When we hear word student or think about student a sketch comes in our mind for student along with its attributes and behaviour. The attributes of student comes in our mind are its name, roll no, class, degree so on. Similarly the behaviour of student comes in our mind are study, register and many more.

We need to capture the characteristic features of any object (attributes and behaviour) in the programming language. The concept of class is used for this purpose.

Now consider the scenario having many interacting objects: a University System having many objects like student, subject, classroom, and teacher so on…we will realize all these objects in our software using classes. These all object will use the services of each other for example student will ask teacher to teach him. This approach is closer to real life instead of having simple functions being called from main here these objects will call each other to get their services. This is the reason we say that object oriented programming mimics real life.

Uses

Objects are structured in terms of class so our problem becomes easier to understand in the terms c++ program.
We can implement interactions easily in terms of classes.

Student objects will interact with each other to take and give services to each other as happens in real life and mapped in object oriented programming approach.

Now we see how class mechanism helps us in implementing real life concept.


We implement generic concepts using types.
We have to model generic concept of Student. But there is no built in type student in c++ like built-in c++ type’s int or float. Class is mechanism in c++ that will allow us to define student as user defined type, similarly generic concept circle will also be implemented in the same way.  User define types will be,

·         Student in student management system
·         Circle in a drawing software

As objects have attributes and behaviour so corresponding classes will also have data members and methods as shown below,

Ali
Corresponding class
Characteristics (attributes)
/*c++ code for class Person, we can create any object like Ali from it*/
class Person  {
private:       /* attributes are generally made private*/
char name[];  /*char array to store name*/
int age; /*int age to store age*/
public:    /* methods are generally made public*/
Person();     /*constructor used to initialize data members*/
void walks();  /* method walk */
void eats();   /*method eats*/
}



Name
Age
Behavior (operations)


Walks
Eats

                         a. object                                                                   b. class code

01.3.     Abstraction

We only include those details in the system that are required for making a functional system so we will leave out irrelevant attributes and behaviour from our objects.
Take the example of student,

Student
·         Name
·         Address
·         Sibling
·         Father Business

01.4.     Defining a New User Defined Type

There are two ways to create user defined types for objects in c++ these are,

Structure Definition:

Partially we can use Structures to define an object

Struct{

};

In c we can not define functions in a structure however in c++ we can add functions in both structure and classes.

Class Definition                                                                                                                          
                                                                          

 




class ClassName 
{                      
                           Access Specifier: (public, private or protected)

                          
                           DataType MemberVariable;
                      … …. …
                           Access Specifier: (public, private or protected)

Function members

 
 

                            ReturnType MemberFunction();
                     … …. …
                       
};
           

Example

class Student
{
      private:

Member Variables
 
     int rollNo;
     char *name;    
     float CGPA;
     char *address;
     

Member Functions
 

      public:
     void setName(char *newName);
     void setRollNo(int newRollNo);

};

Why Member Functions:
They model the behaviors of an object,

Objects can make their data invisible (in accordance with the principle of data hiding).  Setters and getters functions are provided by class to access the its members it also minimizes the changes to move the objects in inconsistent state as we can write checks in our setter functions for example we can check that whether the user has entered correct age value and has not entered negative value for age.
Object remains in consistent state

Example:

We can check that the entered roll number by user is positive or negative,

Student aStudent;
aStudent.rollNo = 514;
aStudent.rollNo = -514;    //Error


Object is an instantiation of a user defined type or class. Once we have defined a class we can create as many objects for that class as we require.

Declaring class variables

Variables of classes (objects) are declared just like variables of structures and built-in data types as follows,

TypeName VariableName;

int var;  // declaring built in int data type variable
Student aStudent;   // declaring user defined class Student object

01.6.     Accessing members

Members of an object can be accessed using,

  1. dot operator (.) to access via the variable name
Student aStudent;   // declaring Student object
                        aStudent. rollNo = 5; 

  1. arrow operator (->) to access via a pointer to an object
Student * aStudent = new Student();  
// declaring and initializing Student pointer
                        aStudent->rollNo = 5;

Note: it is against the principle of OOP to access the data members directly using object of class as we have done above. This code is given for example only we should write assessor functions (setters and getters) wherever we want to access the members of the class.

Member functions are accessed in the similar way using dot or arrow operator.

Example
           class Student{
     int rollNo;
     void setRollNo(int aNo);
       };

     Student aStudent;
     aStudent.setRollNo(5);
     Student *ptr_student = new Student();
     ptr_student->setRollNo(5);


01.7.     Access specifiers
These are used to enforce access restrictions to members of a class, there are three access specifiers,

  1. ‘public’ is used to tell that member can be accessed whenever you have access to the object
  2. ‘private’ is used to tell that member can only be accessed from a member function
  3. ‘protected’ to be discussed when we cover inheritance

Example

class Student{

private:
     char * name;
     int rollNo;
public:
     void setName(char *);
     void setRollNo(int);
...
};

Example Program

class Student{
char * name;
     int rollNo;
public:
     void setName(char *);
void setRollNo(int aNo);
};
void Student::setName(char * aName){
if (strlen(aName) > 0)
      {
           name = new char[strlen(aName)];      
           strcpy(name,aName);
      }
}

void Student::setRollNo(int arollNo){
if(arollNo > 0)
rollNo = arollNo;
}

int main(){
     Student aStudent;
  aStudent.rollNo = 5;
/* Error: we can not access private member of the class. */
  aStudent.name  = “Ali”;
/* Error: we can not access private member of the class */
     aStudent.setRollNo(1);
     aStudent.setName(“Ali”);
/* Correct way to access the data member using public setter functions */
}


Default access specifier

When no access specifier is mentioned then default access specifier is private.

Example

Example

We should use keyword public before the methods of the class as given below of will not use public keyword they will also be treated as private ad will not be accessible outside the class as shown below, 


class Student
{
            char * name;
            int RollNo;
            void SetName(char *);
};
Student aStudent;
aStudent.SetName(Ali);

Corrected code will be,

class Student
{
            char * name;
            int RollNo;
public:
            void setName(char *);
};
Student aStudent;
aStudent.SetName(“Ali”);

Post a Comment

Don't Forget To Join My FB Group VU Vicky
THANK YOU :)

Previous Post Next Post