1
Programming
Languages
Reference 9
Object Oriented
Programming Using C++
C++ History
Dennis Ritchie developed C by at AT&T, Bell Labs in
the 1970s
Many applications written in C
C++ is developed by Bjarne Stroustrup at AT&T, Bell
Labs in the 1980s
Overcame several shortcomings of C
Incorporated object oriented programming
C is a subset of C++
Object Oriented Programming
Object-oriented programming (OOP) is a technique
of programming to write a code in form of
interacting objects
The Coder defines the types of objects
The Coder creates object instances
The Coder specifies the interaction among these
objects
Objects
objects have attributes and behaviors
Examples:
Dog
Attributes: breed, Barking Voice, color, hungry,
tired, etc.
Behaviors: eating, sleeping, etc.
Bank Account
Attributes: account number, owner, balance
Behaviors: withdraw, deposit, Making Demand
Draft
C and C++
In C, data structures and procedures are defined separately.
C, Pascal, FORTRAN, and similar languages are procedural
languages. That is, each statement in the language tells the
computer to do something: Get some input, add these
numbers, divide by six, display that output. A program in a
procedural language is a list of instructions. For very small
programs, no other organizing principle (often called a
paradigm) is needed. The programmer creates the list of
instructions, and the computer carries them out.
In object-oriented languages, they are defined together.
An object is a collection of attributes and the behaviors that
operate on them.
Variables in an object are called attributes.
Procedures associated with an object are called methods.
Problems with Procedural 7
Language
As programs grow ever larger and more complex, even the structured
programming approach begins to show signs of strain. The project is too
complex, the schedule slips, more programmers are added, complexity
increases, costs skyrocket, the schedule slips further, and disaster ensues.
(See The Mythical Man-Month by Frederick P. Brooks, Jr. [Addison Wesley,
1982] for a vivid description of this process.)
Analyzing the reasons for these failures reveals that there are weaknesses
in the procedural paradigm itself. No matter how well the structured
programming approach is implemented, large programs become
excessively complex. What are the reasons for these problems with
procedural languages? There are two related problems. First, functions
have unrestricted access to global data. Second, unrelated functions
and data, the basis of the procedural paradigm, provide a poor model of the
real world.
8
Local and Global Variables
9
Procedural Language
10
Attributes and Behaviors
Attributes
Examples of attributes (sometimes called characteristics)
are, for people, eye color and job title; and, for cars,
horsepower and number of doors. As it turns out, attributes
in the real world are equivalent to data in a program: they
have a certain specific values, such as blue (for eye color) or
four (for the number of doors).
Behavior
Behavior is something a real-world object does in response
to some stimulus. If you ask your boss for a raise, she will
generally say yes or no. If you apply the brakes in a
car, it will generally stop. Saying something and stopping are
examples of behavior. Behavior is like a function: you call a
function to do something (display the inventory,
for example) and it does it.
11
Object Oriented Programming
Approach
12
Objects and Class
13
Inheritance
14
C and C++
15
Program Structure
16
Defining Variables
17
Example
Classes
A class is the generic definition for a set of similar objects (i.e.
Person as a generic definition for Aslam, Akram, Salman
etc.
A class can be thought of as a template used to create a set
of objects.
A class is a static definition; a piece of code written in a
programming language.
One or more objects described by the class are instantiated
at runtime.
The objects are called instances of the class.
Bank Example
The "account" class describes the attributes and behaviors of
bank accounts.
The “account” class defines two state variables (account
number and balance) and two methods (deposit and
withdraw)
Objects
When the program runs there will be many instances of the
account class.
Each instance will have its own account number and balance
(object state)
Methods can only be invoked .
Classes
When classes are defined, programmers can specify that
certain methods or state variables remain hidden inside the
class.
These variables and methods are accessible from within the class, but not
accessible outside it.
The combination of collecting all the attributes of an object into a single
class definition, combined with the ability to hide some definitions and type
information within the class, is known as encapsulation.
Instance Methods and Instance
Variables
The methods and variables described in this module so far are
know as instance methods and instance variables.
These state variables are associated with the one instance of
a class; the values of the state variables may vary from
instance to instance.
Instance variables and instance methods can be public or
private.
It is necessary to instantiate (create an instance of) a class to
use it’s instance variables and instance methods.
Class and Class Variables
In addition to instance methods and instance variables,
classes
can also define class methods and class variables.
These are attributes and behaviors associated with the class
as a whole, not any one instance.
Class variables and class methods can be public or private.
It is not necessary to instantiate a class to use it’s class
variables and class methods.
Inheritance
Derive a new class (subclass) from an existing class
Inheritance creates a hierarchy of related classes (types) that
share code and interface
25
Examples
Base Class Derived Classes
Student CommuterStudent
ResidentStudent
Shape Square
Triangle
Hexagonal
Loan HousePurchaseLoan
HomeImprovementLoan
BikebuyLoan
University Members 26
University Membership Manager
Employee Student
Faculty Staff
chairman Teacher
27
Shape class
Shape
TwoDimensionalShape ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
Subclasses
When a new class is developed a programmer can
define it to
be a subclass of an existing class.
Subclasses are used to define special cases,
extensions, or other variations from the originally
defined class. Sheroo
Generic is
derived
Class for from Dog
Examples: Dog
Sheroo can be defined as With Specific
general Class for
a subclass of Dog. Terrier
attributes
SavingsAccount and With new
and
CheckingAccount can be behaviors attributes
derived from the Account for all and
behaviors
class (see following dogs.
specific
slides). to the
Terrier
4. Implementing Inheritance in
29
C++
Develop a base class called student
Use it to define a derived class called
grad_student
30
The Student Class
student
student_id,
print()
year, name year_group()
batch
inherits
grad_student
dept, print()
ISP
31
Student Class
class student {
public:
student(char* nm, int id, int y);
void print();
int year_group()
{ return year; }
private:
int student_id;
int year;
char name[30];
};
32
Graduate Student Class
class grad_student: public student {
public:
grad_student(char* nm, int id,
int y, char* d, char* th);
void print();
private:
char dept[10];
char thesis[80];
};
33
Use
int main()
{
student s1("Jane Doe", 100, 1);
grad_student gs1("John Smith", 200, 4,
"Pharmacy", "Retail Thesis");
cout << "Student classes example:\n";
cout << "\n Student s1:";
s1.print();
cout << “Year “ << s1.year_group()
<< endl;
:
continued
34
cout << "\n Grad student gs1:";
gs1.print();
cout << “Year “ << gs1.year_group()
<< endl;
:
35
Polymorphism and Overloading
36
endl
Questions