MCA-104, OBJECT ORIENTED PROGRAMMING IN C

++

OBJECT ORIENTED PROGRAMMING IN C++ [UNIT 1]

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 ,By Nitish Pathak

U1.1 1

Learning Objectives of Unit I
• Introduction to Object Oriented Approach • OOPS Relation with other Functional Paradigms • Difference between C and C++

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.2

INTRODUCTION TO OBJECT ORIENTED APPROACH

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.3

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.1

MCA-104, OBJECT ORIENTED PROGRAMMING IN C++

Learning Objectives
• • • • • • • • • • • Principles of Object-Oriented Programming Approach Object Classes Data Abstraction Data Encapsulation Inheritance Polymorphism Dynamic Binding Message Passing POP vs. OOPS Examples of Basic C++ Programs

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.4

OOPS
• • • • • Uses objects & classes (ADT). Data is of primary importance. Latest in S/W development. Most near to the real world. Ultimate paradigm for the modeling of information, be that data / logic. • Overcoming the shortcomings of conventional programming approach.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.5

OBJECT
• An object can be an item, place, person or any other entity. All objects have the following characteristics: Identity: The name associated with an object helps in identifying the object. Example: Play-Ground, Multiplexes, Wallclock, Class-Room, Court-Room.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.6

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.2

MCA-104, OBJECT ORIENTED PROGRAMMING IN C++

OBJECT
• State: An object can be in many state. Example: TV can be in the following states: (a) On State. (b) Off State. © Out of order state.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.7

OBJECT
• Behavior : What the object does or what is it capable of doing? Example: A person can sit, stand, read, sleep, walk & talk etc. Similarly, a cup can be filled, drunk from, washed, broken etc.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.8

OBJECT
• Object is an identifiable entity with some Characteristics & Behavior. Example: Orange is an Object. Characteristics / Data : (1) Spherical shaped (2) Color Orange Behavior / Functions: (1) It is Juicy. (2) It tastes sweet-sour.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.9

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.3

MCA-104, OBJECT ORIENTED PROGRAMMING IN C++

OBJECT
Object: Student Data: Name Class Marks Functions: Total( ) Avg( ) Getdata( ) Writedata( )
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 0

OBJECT
• • • Objects are the basic run time entities in an object oriented system. They match closely with real time objects. Objects take up space in memory and have an associated address like a Record in Pascal and a Structure in C. Objects interact by sending Message to one other. E.g. If “Customer” and “Account” are two objects in a program then the customer object may send a message to the account object requesting for bank balance without divulging the details of each other’s data or code.
U1.1 1

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

CLASS
• A class is a group of objects that share common properties & behavior/ relationships. • In fact, objects are the variables of the type class. • After creating class, one can create any no. of related objects with that class. • Classes are user defined data types and behaves like the built-in types of a programming language.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.1 2

© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak

U1.4

by Nitish Pathak U1. New Delhi-63. Basic Behavior / Functions: setbp( ). Example: fruit mango. New Delhi-63. class employee. OBJECT ORIENTED PROGRAMMING IN C++ CLASS • The syntax used to create an object is similar to create an object integer in C. New Delhi-63.1 3 Class Diagram of Employee class Class: employee Data: Name Dept Desig Basic Functions: Setbp( ) Totsal( ) Deductions( ) © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Class: Employee.5 . totsal( ). New Delhi-63.by Nitish Pathak U1. Desig. Milk Vending machine. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.1 4 ABSTRACTION • It refers to the act of representing essential features without including the background details or explanations.MCA-104.1 5 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. States / Data : Name. Driving a car etc. Railway reservation. Example: Switch Board. deduction( ) © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Dept.

(Not Necessary to know): Internal details like wiring. Steering handling.1 7 ENCAPSULATION Example: Departmental data. [Data Security] © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • Only Relevant details are exposed and rest are made hidden. Weighing Machine etc. ATM cash counter.by Nitish Pathak U1. • Encapsulation is a way to implement data abstraction.by Nitish Pathak U1. New Delhi-63. Engine details & functions.MCA-104. Accelerator etc.by Nitish Pathak U1.1 6 ENCAPSULATION • The wrapping up of data & functions (that operate on the data) into a single unit (called class) is known as ENCAPSULATION.1 8 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. OBJECT ORIENTED PROGRAMMING IN C++ ABSTRACTION • • Explanation (Driving a Car): (Need to Know): Gear handling. Use of Clutch. Brakes.6 .by Nitish Pathak U1. New Delhi-63. New Delhi-63.

e. • Ensures the closeness with real world models.2 1 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Example: A Music system comprises of speaker.e. music. • • • • • Advantages: It reduces the complexity of a system to a greater extent. • Provides Multiple Access Specifiers across the modules (Public. New Delhi-63.2 0 INHERITANCE • Inheritance is the capability of one class of things to inherent properties from other class. Makes data secure. into a set of cohesive and loosely couple modules.by Nitish Pathak U1. New Delhi-63.7 . New Delhi-63. CD player. • Supports the concept of Hierarchial classification. It creates a number of well defined document boundaries within the program. New Delhi-63. Faster speed.by Nitish Pathak U1. Debugging easier. they work in coherence towards achieving one goal i.MCA-104.by Nitish Pathak U1.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ MODULARITY • The act of partitioning a program into individual components i.1 9 Contd. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Though these are different entities in themselves. tuner etc. cassette player. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Private & Protected) © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

• Extensively used in implementing inheritance.by Nitish Pathak U1. • Transitive in nature. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • Multiple Inheritance has other advantages. Example: ‘+’ is used for adding numbers / to concatenate two string / Sets of Union and so on.MCA-104. U1.2 2 POLYMORPHISM / OVERLOADING • A Greek term suggest the ability to take more than one form. • Anti-symmetric also.by Nitish Pathak U1.). New Delhi-63. • It is a property by which the same message can be sent to the objects of different class. OBJECT ORIENTED PROGRAMMING IN C++ INHERITANCE • Supports Reusability that allows the addition of extra features to an existing class without modifying it. Traffic. Army). Run-time dependent.by Nitish Pathak POLYMORPHISM (Contd. Execution depends on the base of a particular definition. New Delhi-63. New Delhi-63. Triangle. • Dynamic Binding/ Late Binding. New Delhi-63.2 4 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.) • Allows to create multiple definition for operators & functions. Move ( Chess.by Nitish Pathak U1. Circle etc. Example: Draw a shape (Box. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.2 3 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.8 .

• It uses ADT (Classes). © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ Advanced Features of OOPS •Generic Programming with Templates Allows several classes which differ only in the datatype of their data members to be declared with a single declaration. • Data is of primary importance. • It uses variables & functions.2 5 POP Vs. New Delhi-63. OOPS • Procedure is of primary importance. New Delhi-63.2 6 Problem Description “ …customers are allowed to have different types of bank accounts. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • Stream Computation Acts as an interface between the program and the input/output devices. withdraw money and transfer money between accounts” © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. • It uses function calls. • It uses various data types. •Exception Handling Mechanism for handling error condition that should not be ignored by a caller.by Nitish Pathak U1.by Nitish Pathak U1.9 . • It uses objects. deposit money.2 7 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. • It uses messages.MCA-104.

2 8 Procedural vs. OBJECT ORIENTED PROGRAMMING IN C++ Procedural Approach cont’d • • • • Focus is on procedures All data is shared: no protection More difficult to modify Hard to manage complexity © Bharati Vidyapeeth’s Institute of Computer Applications and Management. transfer Customer.MCA-104.2 9 Mapping the world to software Objects in the problem domain are mapped to objects in software 011101 10011 0110100 010101 11101 1110101 11010 10101 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. New Delhi-63.by Nitish Pathak U1.10 . deposit. New Delhi-63.by Nitish Pathak U1. money.3 0 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Object-Oriented Procedural Object Oriented Withdraw.by Nitish Pathak U1.by Nitish Pathak U1. New Delhi-63. account © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

3 1 Data Encapsulation class Account { public: float withdraw().by Nitish Pathak U1. New Delhi-63.by Nitish Pathak U1. ).by Nitish Pathak U1. New Delhi-63. OBJECT ORIENTED PROGRAMMING IN C++ Object Oriented Data and operations are grouped together Account Withdraw Deposit Transfer Interface: Set of available operations © Bharati Vidyapeeth’s Institute of Computer Applications and Management. private: float balance. New Delhi-63.3 2 Advantages of Encapsulation • Protection • Consistency • Allows change © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. void deposit(float amount).by Nitish Pathak U1.3 3 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.11 .

3 4 STRUCTURE OF C++ PROGRAM INCLUDE FILES CLASS DECLARATION MEMBER FUNCTION DEFINITIONS [to separate abstract specifications of the interface (class definition) from the implementation details (member function definition)] MAIN FUNCTION PROGRAM © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. •myclass allows the data to be entered (getdata()) and displayed (dispdata()). New Delhi-63.by Nitish Pathak U1.3 5 MYCLASS1.3 6 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.12 . •All functionality of this data member is described through methods (member functions) within the class. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104. New Delhi-63. New Delhi-63.CPP •This class contains one integer num and describes the functionality of this data member.by Nitish Pathak U1. New Delhi-63.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ PROGRAMS IN C++ © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

int num.h> class myclass1// class starts the declaration of a new class { private: public: { cout<<“Enter an integer:”. } }.by Nitish Pathak U1.getdata().h> #include <string. New Delhi-63.by Nitish Pathak U1. We use calc() function to return average salary. void getdata() // to enter the value MYCLASS1. New Delhi-63.CPP // myclass1. b1.CPP void dispdata() // to display the value { cout<<“Num=“ <<num<<endl.by Nitish Pathak U1.dispdata().dispdata(). OBJECT ORIENTED PROGRAMMING IN C++ MYCLASS1. // cin is the keyword to enter data } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.getdata(). b1.MCA-104. */ #include <iostream.cpp #include <iostream. New Delhi-63. New Delhi-63.// cout keyword to display information cin>>num.13 .3 8 Example To Develop An Employee Class /* class employee stores employee information. a1.3 9 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. public: void getdata() // to enter the data © Bharati Vidyapeeth’s Institute of Computer Applications and Management.3 7 //Data members are usually private // Member functions are public in nature. // indicates the end of class command. b1. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. char name[20]. void main() {myclass1 a1.by Nitish Pathak U1. a1. int basic. / /a1 & b1 are objects of class myclass1.h> // to use strings class employee { private: int emp_no.

cout << “Salary: “<< basic.4 2 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. cin>>emp_no. } }.by Nitish Pathak U1.4 0 Example To Develop An Employee Class float calc(employee x) //parameter received { float temp. OBJECT ORIENTED PROGRAMMING IN C++ Example To Develop An Employee Class { cout<<“Enter Employee No:”.by Nitish Pathak U1.getline(name. cout<<“Name:”<< name. /* Object a1 invokes function calc(). } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. b1 is passed as the parameter*/ cout<<endl<<“Average Salary:” <<average.14 .basic)/2. cin.getdata().4 1 Example To Develop An Employee Class void main() { employee a1. cout<<“Enter name:”. New Delhi-63. // End of class declaration © Bharati Vidyapeeth’s Institute of Computer Applications and Management. temp=(float(basic)+x.getdata(). cout<<“Enter Salary: “.MCA-104. New Delhi-63. New Delhi-63. float average = a1. } Void dispdata() // to display the value { cout<<“Employee no:”<<emp_no.by Nitish Pathak U1. b1. cin>>basic.’\n’). © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. b1.//int basic is casted to float return temp.calc(b1). } // end of program. a1. New Delhi-63.

It employs bottom up programming approach. OBJECT ORIENTED PROGRAMMING IN C++ CONCLUSION • OOPS was invented to overcome the drawbacks of the POP. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. polymorphism.4 5 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.by Nitish Pathak U1. inheritance. message passing etc. encapsulation. It treats data a critical element and does not allow it to flow freely around the system.15 .by Nitish Pathak U1. dynamic binding.4 4 Learning Objectives • Programming Techniques In Computing • Evolution of programming paradigm • Types of Programming Paradigms with Abstraction Employed • Merits and demerits of OO Methodology • Application Areas of OOPS © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104.by Nitish Pathak U1. New Delhi-63. New Delhi-63. • Some of the primary features of OOPS are data abstraction.4 3 OOPS RELATION WITH OTHER FUNCTIONAL PARADIGMS © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. • Application testing is generally shorter due to reusability of code that increase programming productivity and decrease maintenance cost.

Problem Characterizing the behavior of discrete system. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.4 6 PROGRAMMING TECHNIQUES IN COMPUTING • • • • • Factors for inherent complexity Complexity of problem domain.by Nitish Pathak U1. Flexibility possible through S/w. • Frequent changes.4 8 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104. New Delhi-63. New Delhi-63. New Delhi-63. Select efficient Language to write effective program. • S/W errors time consuming & expensive.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ PROGRAMMING TECHNIQUES IN COMPUTING • Computer program the most complicated entities created by human. • Programs prone to error.by Nitish Pathak U1.by Nitish Pathak U1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.4 7 PROGRAMMING TECHNIQUES IN COMPUTING To Minimize the Complexity: • • Select appropriate Model suited to the problem at hand. Difficulty in managing the development process.16 . New Delhi-63.

New Delhi-63. New Delhi-63. Hierarchial in nature.by Nitish Pathak U1. New Delhi-63. New Delhi-63. Introduces parameter / arguments concept. Structured / Modular Programming Object Oriented. Programs organized in the form of procedures and all data items are global. • • © Bharati Vidyapeeth’s Institute of Computer Applications and Management.5 0 Procedural Programming • • • Subroutines are abstracted to avoid repetitions.5 1 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ EVOLUTION OF PROGRAMMING PARADIGM • • • • • • Procedure Oriented. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Program controls are through jumps (goto) and call to the subroutine. Constraint Oriented.4 9 Procedural Programming [Example : FORTRAN & COBOL] • Dividing a program into functions / subroutines/ procedures is called Procedure Oriented Programming (POP).MCA-104. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.17 .by Nitish Pathak U1. Rule Oriented. LOGIC Oriented.

New Delhi-63.by Nitish Pathak U1.by Nitish Pathak U1. U1. Minimize Duplication of data. Functions transform data from one form to another. Programs can have global as well as local data and processing logic. the way the data is stored becomes critical. The arrangement of the data can not be changed without modifying all the functions that access it. New Delhi-63. Like other traditional languages. Extensibility ( Creating new data type) is not possible.18 . Each module has a “Set of functions” / “Set of procedures” of related type.5 4 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. money & space. OBJECT ORIENTED PROGRAMMING IN C++ Procedural Programming • • • • • Advantages: Suitable for Medium sized applications. Does not model real world very well. Emphasis on algorithm rather than data. Since many functions access the same data. • • • © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. Saves time. Reduce errors.MCA-104. New Delhi-63.5 2 Procedural Programming • • • Disadvantages: Since global data is accessible to all functions so the data can be accidentally corrupt. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.5 3 Structured/Modular Programming [ Example: PASCAL & C ] • • • • • Program consists of multiple independent modules. Difficult to maintain/enhance the program code.

Employs Top Down approach in program design. Controlled scope of data.5 7 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Supports modular proramming.by Nitish Pathak U1. Introduction of User defined data type. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63.MCA-104. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.by Nitish Pathak U1.5 6 Structured/Modular Programming • • • Disadvantages: Maintenance of a large software system becomes tedious. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. Co-ordination among multiple programs is required for handling the changes made to mutually shared data items. New Delhi-63.19 . OBJECT ORIENTED PROGRAMMING IN C++ Structured/Modular Programming • • • • • Facilitates parameter passing to communicate information.by Nitish Pathak U1.5 5 Structured/Modular Programming • • • Advantages: Good for large programming projects. time consuming & costly. A rich set of control structure are able to further abstract the procedures.

Eiffel. without affecting the operation of the program. [Inheritance]. Ada etc. New Delhi-63.by Nitish Pathak U1.] • • • • Object = Data + Methods. Data is hidden and cann’t be accessed by external function. New Delhi-63. Reusable Code. New Delhi-63. Java. • • • © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Separate necessary from unnecessary details. Abstraction makes it possible to change the data structure of an object. Simula. OBJECT ORIENTED PROGRAMMING IN C++ Object Oriented Programming [Example: C++.20 .5 9 Object Oriented Programming • • • • • • • Advantages: Increased programming productivity. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Problem is divided into Objects rather than Functions. It ties data more closely to the functions and does not allow it to flow freely around the system [Data Abstraction].6 0 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. Decreased maintenance code. Objects communicate by sending message to one another. Greater speed.5 8 Object Oriented Programming • Program ADT and their relationship. Smalltalk.MCA-104. Use Bottom-up program technique for program design. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New data & functions can be easily added whenever necessary.by Nitish Pathak U1. Lesser Complexities.

Better used in AI applications.by Nitish Pathak U1. Computer based Training & Education System. New Delhi-63. OBJECT ORIENTED PROGRAMMING IN C++ Object Oriented Programming • • • • • • Disadvantages Compiler Overhead.6 1 Application Areas of OOPS • • • • • • • • • Real time systems. Requires the mastery of software engineering and programming methodologies.S. Multimedia Design. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Best Practised for hypothesis derivations. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.6 3 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. Simulation & Modelling.by Nitish Pathak U1.6 2 LOGIC ORIENTED PROGRAMMING • • • Goals. Beneficial for large software projects in long run.MCA-104.by Nitish Pathak U1. design. Graphical User Interface. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.21 . Window based O. Re-orientation of software developer to objectoriented thinking. CAD/CAM. New Delhi-63. Object-oriented Operating System. often expressed in Propositional / Predicate Calculus. New Delhi-63. Object-oriented database system. Runtime Overhead.

Example: MYCIN. XCON etc. New Delhi-63. • The state-of-the art solution to overcome software crisis is the object-oriented paradigm. OOP finds its application from design of database system to the future generation operating system which have computing.by Nitish Pathak U1.6 6 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.6 4 Constraint Oriented Programming • Invariant Relationship. OBJECT ORIENTED PROGRAMMING IN C++ Rule Oriented Programming • • • If-then-else Rules Best suited for design of a knowledge base system.6 5 Conclusion • The software crisis provided an impetus for the development of different programming paradigm to build more reliable and reusable system.by Nitish Pathak U1. New Delhi-63.by Nitish Pathak U1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.22 . New Delhi-63. communication and imaging capabilities built into it. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104.by Nitish Pathak U1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

23 .6 9 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.) U1. OBJECT ORIENTED PROGRAMMING IN C++ MOVING FROM C TO C++ © Bharati Vidyapeeth’s Institute of Computer Applications and Management. reference.by Nitish Pathak U1. New Delhi-63. New Delhi-63.6 7 Learning Objectives • • • • • • • • • Comments Strict type checking New Syntax for cast Defining local variables typedef keyword Function as a part of structure Scope Resolution Operator New I/O Stream New variable types (const.by Nitish Pathak U1.by Nitish Pathak U1. New Delhi-63.by Nitish Pathak Learning Objectives • • • • • • • Overloaded procedures Default values to parameters inline procedures Overloaded operators Classes Exceptions Templates © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.6 8 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. etc.MCA-104.

© Bharati Vidyapeeth’s Institute of Computer Applications and Management. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.7 0 Hello World Program using C Using C hello. • `Data hiding'.c: printing Hello World message */ #include <studio.by Nitish Pathak U1. New Delhi-63.24 .by Nitish Pathak U1. the usage of data by one program part while other program parts cannot access the data.cpp //hello. • Programs would be less bug-prone.7 1 Hello World Program using C++ Using C++ hello.by Nitish Pathak U1.MCA-104. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.c /*hello. would be easier to implement with C++.cpp: printing Hello World message #include <iostream.h> void main(){ printf(“Hello world”).7 2 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. • The memory management under C++ would be easier and more transparent. New Delhi-63. New Delhi-63. New Delhi-63. as C++ uses a stricter syntax and type checking.h> void main(){ cout<< “Hello world”. • Creating and using new data types would be easier than in C. OBJECT ORIENTED PROGRAMMING IN C++ C++ Advantages and Claims • New programs would be developed in less time because old code can be reused.

7 5 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. #endif … © Bharati Vidyapeeth’s Institute of Computer Applications and Management.25 . New Delhi-63.by Nitish Pathak U1. New Delhi-63.by Nitish Pathak U1. New Delhi-63.7 4 Stream Based I/O C++’s feature for handling I/O operations are called streams Streams are abstractions that refer to data flow. OBJECT ORIENTED PROGRAMMING IN C++ Comments in C++ Line comments // Avoid C style comments consider using the #if 0 until #endif pair of preprocessor directives … #if 0 cout <<"xx2".7 3 Beginning Comments Heading Author Purpose Usage References File Formats Restrictions Revision History Error Handling Notes Anything else that’ s useful © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104. Stream in C++ are classified into  Output Streams  Input Streams © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. cout <<"xx3".

by Nitish Pathak U1.MCA-104. • Object cin.by Nitish Pathak U1.7 8 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.7 6 Input Streams • Perform read operation with input devices • Performed using cin object • extracts data from a stream and copies the extracted information to variables • skip all white space characters that precede the values to be extracted • Syntax int age.26 .7 7 Input Streams Contd. OBJECT ORIENTED PROGRAMMING IN C++ Output Streams • Perform write operations on output devices • Syntax is cout<< variable • More than one item can be displayed using single output stream object called cascaded output operations cout<< “Age = “<< age. • Input of more than one item can also be performed using the cin input stream object called cascaded input operations cin >> name>> age. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.. New Delhi-63. must be associated with at least one argument © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.by Nitish Pathak U1. cin >>age.by Nitish Pathak U1.

8 0 Void Argument List • Function protype void func(). OBJECT ORIENTED PROGRAMMING IN C++ Points to be noted about the stream operations • Streams do not require explicit datatype specification in I/O statement • Streams do not require explicit address operator prior to the variable in the input stream • Can be extended • Interpreter for function loaded run time © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. if (sizeof('x') == sizeof(int)) cout<<"int". New Delhi-63.by Nitish Pathak U1. / void func(void) • In C first prototype implies argument list is not prototyped not able to warn against improper argument usage & second prototype implies no argument • In C++ both implies same meaning i.MCA-104.e no argument © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. New Delhi-63. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.by Nitish Pathak U1.27 .7 9 Type Compatibility • C++ strict to type compatibility • In C++ char is not promoted to the size of int What is the output? void Fn(){ if (sizeof('x') == sizeof(char)) cout<<"char".8 1 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

New Delhi-63. float average= sum/i. New Delhi-63. New Delhi-63. Saturday.  C++ It is a separate data type enum Weekday { Monday = 1. Weekday day = static_cast <Weekday>(4). Wednesday.8 2 Defining local variables #include <iostream. Thursday.h> int main() { for (int i = 0.by Nitish Pathak U1. i). Friday.by Nitish Pathak U1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.28 .MCA-104. i < 20. i++) printf("%d\n". © Bharati Vidyapeeth’s Institute of Computer Applications and Management. day =8.8 4 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.by Nitish Pathak U1. OBJECT ORIENTED PROGRAMMING IN C++ Declaration of Variables • C++ allows the declaration of a variable anywhere in the scope • Permits initializaion of the variables at run time . Sunday }. float area = 3. Tuesday. return 0.14159*rad*rad. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.8 3 Data Types Enumerated data type  C defines the types of enums to be ints.

New Delhi-63.by Nitish Pathak U1.MCA-104. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. char name[size].by Nitish Pathak U1. C offers the following cast construction:  (typename)expression • C++ also supports the function call notation:  typename(expression) • not actually a cast. • request to the compiler to construct an (anonymous) variable of type typename from the expression expression. New Delhi-63.by Nitish Pathak U1.const int second) {return (first + second).8 6 A new syntax for casts • Traditionally.8 7 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. const allows to create typed constants instead of #define int const size= 10 is equivalent to const int size =10 const in C++ defaults to internal linkage © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.29 .8 5 Constants and Functions Constants can be used in declaring function parameters: int add_two(const int first.} The value of these parameters can not be changed inside this function. OBJECT ORIENTED PROGRAMMING IN C++ Symbolic constant Two ways of creating symbolic constants  Using the qualifier const  Defining a set of integer constants using enum keywords In c++ we can use const in a constant expression const int size = 10. New Delhi-63.

New Delhi-63. New Delhi-63.MCA-104.9 0 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.30 . public: void fn() const { const_cast<sample*>(this)->a = 10.by Nitish Pathak U1. Also used to change data member of a class in const member function class sample { int a.8 8 The `static_cast'-operator used to convert one type to an acceptable other type.by Nitish Pathak U1.8 9 The `const_cast'-operator used to convert const to non-const of a pointer type const int a =5. } }. New Delhi-63.g..  d = static_cast<double>(a) / b. OBJECT ORIENTED PROGRAMMING IN C++ New Style Cast • Standard cast to convert one type to another static_cast<type>(expression) • a special cast to do away with the const typemodification const_cast<type>(expression) • Used to change the interpretation of information: reinterpret_cast<type>(expression) • used in combination with polymorphism dynamic_cast<type>(expression) © Bharati Vidyapeeth’s Institute of Computer Applications and Management. E.by Nitish Pathak U1. int *b = const_cast<int *>(&a). double to int.

New Delhi-63.by Nitish Pathak U1. // this refers to the counter < 10.MCA-104.9 1 The scope resolution operator :: #include <iostream.Reference Variables • Reference variable acts as an alias for the other value variables • enjoys the simplicity of value variable and power of the pointer variable • Syntax Datatype & ReferenceVariable = Value Variable char & ch1 = ch. } return 0. // local variable counter++) { cout<<::counter / counter).by Nitish Pathak U1.by Nitish Pathak U1. // global variable int main() { for (int counter = 1. • dangerous habit • suppresses the normal type c ecking mechanism . New Delhi-63.9 3 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.by Nitish Pathak U1. int *b =reinterpret_cast<int *>a. } // global variable // divided by // local variable © Bharati Vidyapeeth’s Institute of Computer Applications and Management.31 . OBJECT ORIENTED PROGRAMMING IN C++ The `reinterpret_cast'-operator • used to reinterpret pointers int a =25. New Delhi-63.h> int counter = 50.9 2 Variable Aliases. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.h of the compiler © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

cout<<a.h> void main(){ int a = 2.by Nitish Pathak U1. #include <iostream. New Delhi-63.9 5 Reference Variables cont.9 4 Reference Variables contd. cout <<a. x = c.} © Bharati Vidyapeeth’s Institute of Computer Applications and Management. but to the variables to which they are aliased • Function in C++ take arguments passed by reference © Bharati Vidyapeeth’s Institute of Computer Applications and Management.32 .9 6 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. int* b = &a. You can try to. • You cannot get the address of a reference.by Nitish Pathak U1. but what you get is the address of the variable referred to." © Bharati Vidyapeeth’s Institute of Computer Applications and Management. int& x = a..by Nitish Pathak U1. New Delhi-63. b = &c. cout<<a.. • No alias for constant value int &num = 100 //invalid • not bounded to a new memory location. • There is no such thing as "reference arithmetic. int c = 7. • References must be initialized to refer to something.MCA-104. a=5. New Delhi-63. cout <<*b. • There is no such thing as a 0 reference.by Nitish Pathak U1.. OBJECT ORIENTED PROGRAMMING IN C++ Reference Variables cont. New Delhi-63. • Once bound to a variable there is no way to make the reference refer to something else.

Cout<<” b= “<<b. cout << a_count << '\n'. *a = *b. OBJECT ORIENTED PROGRAMMING IN C++ Reference Parameters • Reference may be used in parameter declarations void inc_counter(int &counter){ ++counter.MCA-104. if (x>y) max(a.} © Bharati Vidyapeeth’s Institute of Computer Applications and Management.33 .b) = 425. else cout<<”a=”<<a.} © Bharati Vidyapeeth’s Institute of Computer Applications and Management. return x.9 8 Return by reference An alias for the referred variable Used in operator overloading void main(){ int a. int*b){ int temp = *a.by Nitish Pathak U1. #include <iostream. { cin>>a>>b. return y.b. New Delhi-63.b>:”.} For example: main(){ int a_count = 0. // Random counter inc_counter(a_count). b = temp. cout<< i <<" "<<j<<"\n".9 7 #include <iostream. swap2(i. int& b){ int temp = a. New Delhi-63. a = b. *b = temp.} void main(){ int i =5. cout<< i <<" "<<j<<"\n".by Nitish Pathak U1.by Nitish Pathak U1.} } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. cout<< i <<" "<<j<<"\n".c. return (0).} void swap2(int & a.h> int & max(int & x. New Delhi-63.h> void swap1(int * a. return.9 9 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. swap1(&i. j). int j = 10. &j). int & y) cout<<”enter <a. return.by Nitish Pathak U1.

but prohibit the caller from changing it. char const *adr).1 00 Problem in program Segment const int &min(const int &i1. /* etc. /* some functions to manipulate PERSON_ structs */ /* initialize fields with a name and address */ void initialize(PERSON_ *p.by Nitish Pathak U1. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. /* print information */ void print(PERSON_ const *p). return (0).by Nitish Pathak U1.1 02 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Then we use a constant reference return: const int & max(int & x. New Delhi-63. } PERSON_.. OBJECT ORIENTED PROGRAMMING IN C++ Constant Reference Returns • Suppose we want to return the Max element. char const *nm.1 01 Structs in C /* definition of a struct PERSON_ */ typedef struct { char name[80]. 3+4). return (i2).34 .by Nitish Pathak U1. New Delhi-63.MCA-104. char address[80]. */ © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.by Nitish Pathak U1.const int &i2) { if (i1 < i2) return (i1). int & y) © Bharati Vidyapeeth’s Institute of Computer Applications and Management. } int main(){ int &i = min(1+2.

"some address"). }. "some address"). © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. New Delhi-63. initialize(&x.1 05 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.initialize("some name"..MCA-104. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. structs in C++ //IN C PERSON_ x. // The name of the user • Assigning the string a value: my_name = "Oualline". OBJECT ORIENTED PROGRAMMING IN C++ Struct or class in C++ class Person { public: void initialize(char const *nm. Full_name = first_name + " " + last_name.by Nitish Pathak U1.by Nitish Pathak U1. char d_address[80]. void print().1 03 Structs in C vs. x.1 04 C++ Strings • Bring in the string package using the statement: #include <string> • Declaring a string std::string my_name. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63.35 . char const *adr). //IN C++ Person x.by Nitish Pathak U1. • Using the “ +” operator to concatenate strings: first_name = "Steve". private: char d_name[80]. New Delhi-63. // etc. "some name". last_name = "Oualline".

New Delhi-63.1 07 C++ string to c-style string #include <iostream> #include <string> using namespace std. stringTwo = stringOne.36 . string stringTwo. // using plain ascii-Z string stringTwo(stringOne). char const *cString = stringOne.1 08 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. int main() { string stringOne("Hello World"). // assign stringOne to stringTwo stringTwo = "Hello world". // using another string object string stringThree. return 0.c_str().by Nitish Pathak U1. New Delhi-63. New Delhi-63.by Nitish Pathak U1. // assign a C-string to StringTwo return 0.MCA-104. int main() { string stringOne("Hello World"). // implicit initialization to "" } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. OBJECT ORIENTED PROGRAMMING IN C++ String Initialization #include <string> using namespace std.by Nitish Pathak U1.1 06 String Assignment #include <string> using namespace std. int main() { string stringOne("Hello World").by Nitish Pathak U1. cout << cString << endl. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

sub = str.1 09 More on Strings Extract a substring: result = str. if (stringOne. } © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.37 . each of which can be either coded directly or further decomposed. // now "Hello world" if (stringOne[0] == 'H') stringOne[0] = 'h'. last).at(0) == 'H') stringOne. // 01234567890123 str = "This is a test". • In C++. New Delhi-63. // sub == “567890” Finding the length of a string string.at(0).by Nitish Pathak U1. until they are directly code-able as single C++ functions. stringTwo. • These functions are combined into other functions and are ultimately used in main() to solve the original problem.1 11 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. This unit can be invoked from other parts of the program.6).by Nitish Pathak U1.by Nitish Pathak U1. return 0. is a software engineering method of stepwise refinement.substr(5. OBJECT ORIENTED PROGRAMMING IN C++ String element … string stringOne("Hello World"). // now "hello world" stringTwo = "Hello World". a function must be defined before it can be used any where in the program. New Delhi-63. New Delhi-63.by Nitish Pathak U1.at(6) = stringOne.1 10 Functions • A function is a named unit of a group of program statements. stringOne[6] = 'w'.length() © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.at(0) = 'W'. • Decomposing difficult problems.substr(first.MCA-104. • A programmer can solve a simple problem in C++ with a single function but difficult and complex problems can be decomposed into sub-problems.

New Delhi-63. int b) . • A function prototype is a declaration of the function that tells the program about the type of value returned by the function and the number & type of arguments passed. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 14 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. } • where datatype specifies the type of value the function returns (e. char.g. • A function definition is automatically a declaration. by the name area(). New Delhi-63. New Delhi-63.by Nitish Pathak U1. float. OBJECT ORIENTED PROGRAMMING IN C++ Functions • The general function definition is as given below: datatype function-name (parameter list) { function body . • If the parameter list is empty. the compiler assumes it as default integer type.by Nitish Pathak U1.by Nitish Pathak U1. double. It then lets the program compute the result by sending both the values to a function. the compiler assumes that the function does not take any arguments.by Nitish Pathak U1.38 . © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 12 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. If no datatype is mentioned. int area(int a. New Delhi-63. user-defined). which are the variables that are sent to the function to work on. • The parameter list is also known as the arguments or signature of the function.: int.1 13 Functions • The above program allows the user to input two integer values.MCA-104.

hence. the function makes its own copy of the argument and then uses them. pass-byvalue and pass-by-reference . it insures the safety of the data. New Delhi-63. which occupies some memory space thus increasing the size of the program.by Nitish Pathak U1.by Nitish Pathak U1. New Delhi-63.1 17 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1.by Nitish Pathak U1. • The default values must to be specified at the time of definition. • The main benefit of call-by-value method is that the original copy of the parameters remains intact and no alteration by the function is reflected on the original variable. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. OBJECT ORIENTED PROGRAMMING IN C++ Default Arguments • C++ allows us to assign default values to function parameters. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 15 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. that is. which are used in case a matching argument is not passed in the function call statement.39 . All execution is done on the formal parameters. • The drawback of call-by-value is that a separate copy of the arguments is used by the function. New Delhi-63.1 16 Pass-by-Value • A function can be invoked in two manners viz.MCA-104. The pass-by-value method copies the actual parameters into the formal parameters.

OBJECT ORIENTED PROGRAMMING IN C++ © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 19 Call-By-Reference • The call by reference uses a different mechanism. then the formal parameters become reference to the actual parameters in the calling function.MCA-104. • This called function works with the original data and any change in the value is reflected back to the data.1 18 The figure shows that the original parameters are copied into the formal parameters in the PrintSumAve() function and then executed in the function body. rather.40 .by Nitish Pathak U1. • A reference is an alias for the predefined variable.by Nitish Pathak U1. New Delhi-63. New Delhi-63. • When a function is called by reference.by Nitish Pathak U1. a reference to the original variable is passed. This means that. New Delhi-63. which is called. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. in call by reference method. In place of passing value to the function. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. the called function does not create its own copy of the original values. That is.by Nitish Pathak U1.1 20 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. the value of that variable can be accessed by using any of the two: the original or the reference variable name. it refers to the original values only by different names.

New Delhi-63.1 23 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. we have to pass parameters by reference.by Nitish Pathak © Bharati Vidyapeeth’s Institute of Computer Applications and Management.MCA-104. New Delhi-63. double &) . double &. which returns multiple output.41 .1 22 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. double. the change is reflected back in the actual variables in the called function. • • The address (reference) of the actual variable is passed to the function. New Delhi-63.by Nitish Pathak U1. void SumAverage (double. OBJECT ORIENTED PROGRAMMING IN C++ Call-By-Reference To write a function. instead of its value. New Delhi-63. U1. since they share the same memory location.by Nitish Pathak U1. If the function changes the parameter’s value.1 21 Call-By-Reference in Functions We use & to denote a parameter that is passed by reference: <data type> &<variable name> Example: void Increment(int &Number) .

the function change() refers to the original value of orig. New Delhi-63. New Delhi-63. is reflected in orig also.MCA-104. OBJECT ORIENTED PROGRAMMING IN C++ © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. • Hence the change in a .by Nitish Pathak U1.1 24 Call by Reference • In the above program. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.42 . which is 10. • The same memory location is referred to by orig in main() and by a in change(). by its reference a.by Nitish Pathak U1.by Nitish Pathak U1. New Delhi-63.1 25 Pass-by-value Vs Pass-by-reference © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 26 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. by assigning value20.

as C++ uses a stricter syntax and type checking.y a it changes when implemented using pass b . OBJECT ORIENTED PROGRAMMING IN C++ © Bharati Vidyapeeth’s Institute of Computer Applications and Management. one using pass b .by Nitish Pathak U1.by Nitish Pathak U1. would be easier to implement with C++.MCA-104.y reference method. when it is evaluated using pass b v lue method and on the other hand . New Delhi-63.by Nitish Pathak U1.y value method and the other using pass b .y reference method. New Delhi-63. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63.1 27 Pass by Value / Pass by Reference This program uses two functions.43 . • The memory management under C++ would be easier and more transparent. the usage of data by one program part while other program parts cannot access the data.1 29 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • `Data hiding'. This shows how the value of number does not change in the main function. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. New Delhi-63. • Creating and using new data types would be easier than in C.1 28 C++ Advantages • New programs would be developed in less time because old code can be reused. • Programs would be less bug-prone.

would be easier to implement with C++. generic classes.1 32 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. • It has efficient memory management techniques and a new style of program analysis and design that provides a foundation for data abstraction. encapsulation. New Delhi-63.MCA-104. high performance computing and system programming applications. New Delhi-63.1 31 Summary • OOP is generally useful for any kind of application but it is particularly suited for interactive computer graphics. as C++ uses a stricter syntax and type checking. simulations. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. artificial intelligence. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. polymorphism. exception handling. OBJECT ORIENTED PROGRAMMING IN C++ Conclusion • New programs would be developed in less time because old code can be reused. stream computation etc. • `Data hiding'.by Nitish Pathak U1. inheritance. • Programs would be less bug-prone.44 .by Nitish Pathak U1. the usage of data by one program part while other program parts cannot access the data. New Delhi-63. New Delhi-63. • The memory management under C++ would be easier and more transparent.by Nitish Pathak U1. databases.1 30 Summary • The C++ language evolved as a result of extensions and enhancements to C. • Creating and using new data types would be easier in C++ than in C.

5.MCA-104.45 . What is the advantage of separating an interface from its implementation? 9.riented language? 8. why did C++ become more popular? © Bharati Vidyapeeth’s Institute of Computer Applications and Management. OBJECT ORIENTED PROGRAMMING IN C++ Review Questions [Objective Types] 1. 3. 4. Which is the first object oriented language? Explain the heritage of C++. What is the purpose of abstraction in C++? 3. How software crisis justifies the need for a new paradigm? Explain the various features of OO paradigm? 2. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. What that does mean in programmer’s language? © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Write an object representation (pictorial) of student class. What is the difference between Visual Basic and Visual C++? 6. What is the concept of multiple inheritance? 10. Is it necessary to use encapsulation feature to create class? 5. Inspite of so many object oriented languages. New Delhi-63.by Nitish Pathak U1. New Delhi-63. Why do people change over from structured programming to object programming approach? 4. What is the difference between inheritance and delegation? Illustrate with example. I keep hearing that in structured programming data is given a step motherly treatment and the whole emphasis on doing thing.by Nitish Pathak U1.by Nitish Pathak U1.by Nitish Pathak U1. List different methods of realizing polymorphism and explain them with examples.1 34 Review Questions [Short Answer Types] 1. New Delhi-63. Is it appropriate to call C++ as “better C”? 2.1 33 Review Questions [Objective Types] 7.1 35 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. What is the difference between an object based language and an object o .

4. OBJECT ORIENTED PROGRAMMING IN C++ Review Questions [Short Answer Types] 6. Enumerate the important features of stream based I/O and provide a comparative analysis with its C counterpart statements such as scanf() and printf(). New Delhi-63. How does a main() functions in C++ differ from main() in C? 2.by Nitish Pathak U1. New Delhi-63. What is name mangling? Is this transparent to user? 9.MCA-104. 6.by Nitish Pathak U1. What are generic classes? Explain how they are useful? Write an interactive program having template based Distance class. New Delhi-63.1 37 Review Questions [Long Answer Types] 5. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.by Nitish Pathak U1. What are the differences between reference variables and normal variables? Why can not constant value be initialized to variables of a reference type? 10. What is polymorphism? Write a program to overload the + operator for manipulating objects of the Distance Class. Describe the major parts of a C++ program. Explain its need with the help of a suitable example. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. What are the different types of access specifiers supported by C++. 3. What are the differences between static binding and late binding ? Explain dynamic binding with a suitable example.46 . Why are variables defined with const called as readonly variable? What are its benefits when compared to macros? 8. New Delhi-63. Illustrate the use of inline function in A C++ program? How it is different from MACROS? List its advantages and disadvantages also. 7. Explain the need of type conversion with suitable examples.by Nitish Pathak U1. 7.1 38 © Bharati Vidyapeeth’s Institute of Computer Applications and Management.1 36 Review Questions [Long Answer Types] 1. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Explain the constructs supported by C++ to implement them. What is inheritance? What are base class and derived class? Give a suitable example for inheritance 8. List the various object oriented features supported by C++. Create two objects: one of type integer and another of type float.

Both source and destination files have to be processed as the objects of the file stream class. Vikas Publication.Venugopal. “Object Oriented Programming using C++”. “C++ Primer”. Lippman & J. What are exceptions? What are the constructs supported by C++ to handle exception. OBJECT ORIENTED PROGRAMMING IN C++ Review Questions [Long Answer Types] 9. BPB Publication. New Delhi-63. A.MCA-104. S. B.by Nitish Pathak U1. 2009. Ravishanker “Mastering C++”. What are streams? Write an interactive program to copy a file to another file.47 .by Nitish Pathak U1.1 39 Recommended Books TEXT: 1. 3. © Bharati Vidyapeeth’s Institute of Computer Applications and Management. Steven C. 2. 2008. T. “The Art of Programming Computer Science with C++”. Galgotia Publications. R. D . Schildt Herbert. Lawlor.1 40 © Bharati Vidyapeeth’s Institute of Computer Applications and Management. New Delhi-63. “C++: The Complete Reference”. Lafore. 2. 10. New Delhi-63. Lajoie. Parasons. Addison Wesley. 4. REFERENCE: 1. 7th Ed. 6th Edition. 2006. “Object Oriented Programming with C++”..R. 2008. Rajkumar.by Nitish Pathak U1.. Tata McGraw Hill. TMH. © Bharati Vidyapeeth’s Institute of Computer Applications and Management.

Sign up to vote on this title
UsefulNot useful