Classes Objects
Classes Objects
Lecture Outline
Procedural Languages
e.g. C, Pascal, Fortran Structured programming is a programming paradigm that to a large extent relies on the idea of dividing a program into functions and modules. A program is basically a list of instructions As programs become larger -> broken down into smaller units, such as functions, procedures, subroutines Functions can be grouped together into modules according to their functionality, objectives and tasks.
global data X
global data Y
global data Z
program structure difficult to conceptualize difficult to modify and maintain the program e.g. : it is difficult to tell which functions access the data
Because complex real world objects have both; attributes people: name, date of birth, eye color, job title cars:number of seats, number of doors, color behaviours people: ask a person to drive the car cars: apply the brakes, turn on the engine sufficient to realistically model real world objects but a unified view is needed
data
Encapsulation: integrate data and functions into one object Data hiding : data is hidden to the outside world and can only be accessed via the functions
data
data
Characteristics of OOP
Objects Classes Inheritance Reusability Creating new data types Polymorphism & Overloading
class
A class can be considered as a more complex data structure than an ordinary built-in data type already know the struct command for user defined data types: struct complex { double re; double im; }; complex x;
Examples of Objects
physcial objects vehicles in a traffic-flow simulation electrical components in a circuit-design program data-storage constructs arrays stacks human entities employees students collections of data an inventory an address book user defined data types time complex numbers
Inheritance
In our daily lives we use the concept of classes divided into subclasses, for example vehicles are divided into cars, trucks, buses and motor cycles. The principle in this sort of division is that each sub-class shares some common features with the base class from which it is derived, but also has its own particular features. base class Vehicle wheels engine Car Truck wheels wheels engine sub-classes or engine trunk derived classes trailer
Inheritance
A sub-class also shares common methods with its super-class but can add its own methods or overwrite the methods of its super-class.
base class
Inheritance
Terminology: Car is a sub-class (or derived class) of Vehicle
Reusability
The concept of inheritance provides an important extension to the idea of reusability, as it allows a
programmer
to
take
an
existing
class
without
modifying it and adding additional features and functionality. This is done by inheriting a new sub-class from the exisiting base class.
An object has the same relationship to a class that a variable has to a data type. An object is said to be an instance of a class. Think of a class as a blueprint, and the object is the actual, tangible thing, or object.
The definition of a class starts with the keyword class, followed by the class name. Just like a structure, the body of a class is delimited by braces and terminated by a semicolon.
class time //define a class { private: int hr, min, sec; //class data public: void setdata(int h, int m, int s) { hr = h; min=m; sec=s; } void showdata() { cout << "Hour: " << hr << endl; cout << "Min: " << min << endl; cout << "Sec: " << sec<< endl; }
};
t1.showdata(); t2.showdata(); }
A key feature in object-oriented languages is data hiding. This term data hiding, means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside of the class. To hide data, you put it in a class and make it private.
Private data or functions can only be accessed from within a class. Public data or functions, on the other hand, are accessible from outside the class.
Class Data
The time class in our example contains 3 data items: hr, min, sec, which are of type int. The data items within a class are called data members. There can be any number of data members in a class, just as there can be any number of data items in a structure.
Member Functions
Member Functions are functions that are included within classes. In our last example we had 2 member functions, setdata() and showdata(). You can define the member functions in the class or more common is outside of your class which we will see later on.
Member Functions
In our last example the member functions follow the keyword public. Since they are declared as public, they can be accessed from outside of the class.
Usually the data within a class is private and the functions are public. This is a result of the way classes are used.
The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed outside of the class.
However there is no rule that says data must be private and functions public.
Now that we have the class defined, lets see how main() will take use of it. In our last example, the first statement in main() was time t1, t2; This statement defines two objects of class time.
Remember that the definition of the class does not create any objects. The definition only describes how they will look when they are created. Objects are what participate in program operations. Defining an object is similar to defining a variable of any data type, space is set aside for it in memory.
In our example, the next 2 statements in main() call the member function setdata(): t1.setdata(6, 12, 45); t2.setdata(2,9,30);
This strange syntax is used to call a member function that is associated with a specific object.
Because setdata() is a member function of the time class, it must always be called in connection with an object of this class.
To use a member function, the dot operator connects the object name and the member function. The syntax is similar to the way we refer to structure members.
Recommended Reading
Robert Lafore, Chapter 1: The Big Picture Dietel & Dietel, Chapter 3: Introduction to Classes and Objects