Escolar Documentos
Profissional Documentos
Cultura Documentos
CONTENTS
1. Designing Software
2. Object Orientation
3. Object Oriented Programming in C++
4. Summary of OOP
5. References
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 1 of 23
OBJECT ORIENTED PROGRAMMING IN C++
DESIGNING SOFTWARE
Goals
In this section you will learn:
v Why software systems need to be designed.
v What the steps in software life cycle are.
v What the different software design paradigms are.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 2 of 23
OBJECT ORIENTED PROGRAMMING IN C++
BENEFITS OF MODELING
A model is a simplification of reality.
We build models so that we can better understand the system we are building.
AIMS OF MODELING
1. Models help us to visualize a system as it is or as we want it to be.
2. Models permit us to specify the structure or behavior of the system.
3. Models give us a template that guides us in constructing a system.
4. Models document the decisions we have made.
PRINCIPLES OF MODELING
1. The choice of what models to create has a profound influence on how a problem is attacked and how a
solution is shaped.
2. Every model may be expressed at different levels of precision.
3. The best models are connected to reality.
4. No single model is sufficient. Every non-trivial system is best approached through a small set of nearly
independent models.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 3 of 23
OBJECT ORIENTED PROGRAMMING IN C++
OBJECT ORIENTATION
Goals
In this section you will learn:
v What Object Oriented Programming (OOP) is.
v Master the vocabulary, rules and idioms of OOP.
Role of Object Orientation
v A software design tool: As a tool for Object Oriented Analysis and Design (OOAD). Can be used
independent of a programming language. Example: Unified Modeling Language (UML).
v A software implementation tool: As a tool for implementing object oriented designs in software, called
as Object Oriented Programming (OOP). A number of programming languages: C++, Java, C#,
Object Pascal, Smalltalk, Simula.
Possible Reasons for Current Popularity of OOP [Budd, 1997]
v The hope that it will quickly and easily lead to increased productivity and improved reliability.
v The desire for an easy transition from existing languages.
v The resonant similarity to techniques of thinking about problems in other domains.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 4 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 5 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 6 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 7 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 8 of 23
OBJECT ORIENTED PROGRAMMING IN C++
7. Member data referenced in the member functions belongs to the object on behalf of whom the member
function is invoked.
a. The object invoking the member function will be known only when the program is written and not when
the class is being defined.
b. In the expression (b*b) – (4*a*c), a, b, and c belong to the object on behalf of whom the member
function discriminant() will be invoked. The object that will invoke the member function will be only
when this function is actually called in the main()or any other function.
void main()
{ quadEqn eq1, eq2(2, 3, -1), eq3(eq2);
eq1.show();
eq2.show();
eq3.show();
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 9 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Note
The name of the member function must be fully qualified when written outside the class definition, otherwise it will
be mistaken for a non-member function.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 10 of 23
OBJECT ORIENTED PROGRAMMING IN C++
OPERATOR OVERLOADING
v The aim of the C++ class concept is to provide he programmer with a tool for creating new types that can be
used as conveniently as the built-in types [Stroustrup, 2000].
v Built-in data types have the following features:
o Assignment, Mathematical and logical operators are defined for them.
o Input and output streams can be used for data input and output.
v Similar features can be extended to User Defined Types through OPERATOR OVERLOADING.
v This is made possible by:
o considering operations as function calls, and
o permitting co-existence of multiple functions with the same name but different parameters and/or
return values.
v The operation a + b is performed by the function call a.operator+(b) or alternately the function call
operator+(a, b). In the first, operator+() is a member function invoked on behalf of the first operand a
with second operand b supplied as the argument to the function. In the second, operator+() is a
non-member friend function and both operands a and b are supplied as arguments to the function.
v Programmer can define these operator functions relevant to the class being defined.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 11 of 23
OBJECT ORIENTED PROGRAMMING IN C++
ANOTHER EXAMPLE
class Complex
{
private:
double re, im; // real and imaginary parts of a complex number
public:
Complex() { re = im = 0; } // null constructor
// parameterized constructor. Can convert a real number into a complex number
Complex(double r, double i=0) { re = r; im = i; }
Complex(const Complex &x) { re=x.re; im=x.im; } // copy constructor
~Complex() { };
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 12 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 13 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 14 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 15 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 16 of 23
OBJECT ORIENTED PROGRAMMING IN C++
void employee::print()
{ cout << “Employee name: “ << fname << “ “ << lname << endl;
}
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 17 of 23
OBJECT ORIENTED PROGRAMMING IN C++
void manager::print()
{ cout << “Manager name: “ << fname << “ “ << lname << endl;
cout << “Number of subordinates: “ << numSub << endl;
}
void main()
{ employee emp(“raj”, “”);
manager mgr(“ajay”, “”, 5);
emp.print();
mgr.print();
}
1. An additional member data int numSub is added to the class manager.
2. If void print() were not defined in class manager, it would have inherited it from class employee.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 18 of 23
OBJECT ORIENTED PROGRAMMING IN C++
POLYMORPHISM
v Inheritance provides new functionality but adds to the complexity.
void main()
{ employee emp(“raj”, “”);
employee *mgr = new manager(“ajay”, “”, 5);
emp.print();
mgr->print();
}
v When dynamic memory allocation is used and objects are addressed through pointers, class of the object is
determined at run time rather than at compile time.
v Assuming that an employee pointer always points to an employee object leads to the incorrect call of
print() of employee instead of print() of manager.
v Polymorphism mechanism can solve this problem.
class employee
{
protected:
char *lname, *fname;
public:
virtual void print(); // virtual functions implement polymorphism
};
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 19 of 23
OBJECT ORIENTED PROGRAMMING IN C++
Shape
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 20 of 23
OBJECT ORIENTED PROGRAMMING IN C++
TEMPLATES
v Another mechanism for reuse is the TEMPLATE. Templates are parameterized classes.
v One class definition creates one new data type.
v One template class definition creates a series of classes, each of which is a new class.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 21 of 23
OBJECT ORIENTED PROGRAMMING IN C++
SUMMARY
1. Everything is an object.
2. Computation is performed by objects communicating with each other, requesting that other objects perform
actions. Objects communicate by sending and receiving messages. A message is a request for action bundled
with whatever arguments may be necessary to complete the task.
3. Each object has its own memory, which consists of other objects.
4. Every object is an instance of a class. A class simply represents a grouping of similar objects, such as integers
or lists.
5. The class is the repository for behavior associated with an object. That is, all objects that are instances of the
same class can perform the same actions.
6. Classes are organized into a singly rooted tree structure, called the inheritance hierarchy. Memory and behavior
associated with instances of a class are automatically available to any class associated with a descendant in
this tree structure.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 22 of 23
OBJECT ORIENTED PROGRAMMING IN C++
REFERENCES
C Programming Language
• Kernighan, B.W. and Ritchie, D.M., The C Programming Language, 2ed., Prentice Hall of India Pvt. Ltd., New
Delhi, 1988.
• Johnsonbaugh, R. and Kalin, M., Applications Programming in C, Maxwell Macmillan International Editions,
Macmillan Publishing Co., New York, 1990.
C++ Programming Language
• Stroustrup, B., The C++ Programming Language, Special Edition, Addison-Wesley, Reading, Massachusetts,
2000.
• Hubbard, J.R., Schuam’s Outline of Theory and Problems of Programming with C++, 2ed., McGraw-Hill Book
Co., New York, 2000.
• Schildt, H., C++: The Complete Reference, Tata McGraw-Hill Publishing Co. Ltd., New Delhi, 1998.
• Lippman, S.B. and Lajoie, J., C++ Primer, 3ed., Addison-Wesley, Reading, Massachusetts, 2000.
Computer Applications to Civil Engineering Problems AICTE–ISTE Short Term Course, MCE, Hassan
Slide 23 of 23