Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63 ,By Nitish Pathak
U1.1 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1
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 Vidyapeeths 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 Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2
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 Vidyapeeths 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 Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3
OBJECT
Object: Student Data: Name Class Marks Functions: Total( ) Avg( ) Getdata( ) Writedata( )
Bharati Vidyapeeths 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 others data or code.
U1.1 1
Bharati Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4
CLASS
The syntax used to create an object is similar to create an object integer in C. Example: fruit mango; class employee; Class: Employee; States / Data : Name, Dept, Desig, Basic Behavior / Functions: setbp( ), totsal( ), deduction( )
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 3
ABSTRACTION
It refers to the act of representing essential features without including the background details or explanations. Example: Switch Board, Railway reservation, Milk Vending machine, Driving a car etc.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5
ABSTRACTION
Explanation (Driving a Car): (Need to Know): Gear handling, Steering handling, Use of Clutch, Brakes, Accelerator etc. (Not Necessary to know): Internal details like wiring, Engine details & functions.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,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. Encapsulation is a way to implement data abstraction. Only Relevant details are exposed and rest are made hidden. [Data Security]
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 7
ENCAPSULATION
Example: Departmental data, ATM cash counter, Weighing Machine etc.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6
MODULARITY
The act of partitioning a program into individual components i.e. into a set of cohesive and loosely couple modules. Example: A Music system comprises of speaker, cassette player, CD player, tuner etc. Though these are different entities in themselves, they work in coherence towards achieving one goal i.e. music.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 9
Contd.
Advantages: It reduces the complexity of a system to a greater extent. It creates a number of well defined document boundaries within the program. Makes data secure. Faster speed. Debugging easier.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 0
INHERITANCE
Inheritance is the capability of one class of things to inherent properties from other class. Supports the concept of Hierarchial classification. Ensures the closeness with real world models. Provides Multiple Access Specifiers across the modules (Public, Private & Protected)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7
INHERITANCE
Supports Reusability that allows the addition of extra features to an existing class without modifying it. Transitive in nature. Anti-symmetric also. Multiple Inheritance has other advantages.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 2
POLYMORPHISM / OVERLOADING
A Greek term suggest the ability to take more than one form. It is a property by which the same message can be sent to the objects of different class. Example: Draw a shape (Box, Triangle, Circle etc.), Move ( Chess, Traffic, Army).
U1.2 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
POLYMORPHISM (Contd.)
Allows to create multiple definition for operators & functions. Example: + is used for adding numbers / to concatenate two string / Sets of Union and so on. Dynamic Binding/ Late Binding. Run-time dependent. Execution depends on the base of a particular definition. Extensively used in implementing inheritance.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 6
Problem Description
customers are allowed to have different types of bank accounts, deposit money, withdraw money and transfer money between accounts
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 8
Procedural
Object Oriented
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.2 9
011101 10011
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.10
Object Oriented
Data and operations are grouped together
Account
Withdraw Deposit Transfer
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 1
Data Encapsulation
class Account { public: float withdraw(); void deposit(float amount); private: float balance; );
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 2
Advantages of Encapsulation
Protection Consistency Allows change
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.11
PROGRAMS IN C++
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 5
MYCLASS1.CPP
This class contains one integer num and describes the functionality of this data member. All functionality of this data member is described through methods (member functions) within the class. myclass allows the data to be entered (getdata()) and displayed (dispdata()).
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.12
MYCLASS1.CPP
// myclass1.cpp #include <iostream.h> class myclass1// class starts the declaration of a new class { private: public: { cout<<Enter an integer:;// cout keyword to display information cin>>num; // cin is the keyword to enter data }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 7
//Data members are usually private // Member functions are public in nature.
MYCLASS1.CPP
void dispdata() // to display the value { cout<<Num= <<num<<endl; } }; // indicates the end of class command. void main() {myclass1 a1, b1; / /a1 & b1 are objects of class myclass1. a1.getdata(); b1.getdata(); a1.dispdata(); b1.dispdata(); }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.3 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.13
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.14
CONCLUSION
OOPS was invented to overcome the drawbacks of the POP. It employs bottom up programming approach. It treats data a critical element and does not allow it to flow freely around the system. Some of the primary features of OOPS are data abstraction, encapsulation, inheritance, polymorphism, dynamic binding, message passing etc. Application testing is generally shorter due to reusability of code that increase programming productivity and decrease maintenance cost.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.15
PROGRAMMING TECHNIQUES IN COMPUTING Computer program the most complicated entities created by human. Frequent changes. Programs prone to error. S/W errors time consuming & expensive.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 6
PROGRAMMING TECHNIQUES IN COMPUTING Factors for inherent complexity Complexity of problem domain. Difficulty in managing the development process. Flexibility possible through S/w. Problem Characterizing the behavior of discrete system.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 7
PROGRAMMING TECHNIQUES IN COMPUTING To Minimize the Complexity: Select appropriate Model suited to the problem at hand. Select efficient Language to write effective program.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.16
Procedure Oriented. Structured / Modular Programming Object Oriented. LOGIC Oriented. Rule Oriented. Constraint Oriented.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.4 9
Procedural Programming
[Example : FORTRAN & COBOL] Dividing a program into functions / subroutines/ procedures is called Procedure Oriented Programming (POP). Programs organized in the form of procedures and all data items are global. Program controls are through jumps (goto) and call to the subroutine.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 0
Procedural Programming
Subroutines are abstracted to avoid repetitions. Hierarchial in nature. Introduces parameter / arguments concept.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.17
Procedural Programming
Advantages: Suitable for Medium sized applications. Minimize Duplication of data. Reduce errors. Saves time, money & space.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 2
Procedural Programming
Disadvantages:
Since global data is accessible to all functions so the data can be accidentally corrupt. Since many functions access the same data, 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. Like other traditional languages, Extensibility ( Creating new data type) is not possible. Difficult to maintain/enhance the program code. Does not model real world very well.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 3
Structured/Modular Programming
[ Example: PASCAL & C ] Program consists of multiple independent modules. Each module has a Set of functions / Set of procedures of related type. Emphasis on algorithm rather than data. Programs can have global as well as local data and processing logic. Functions transform data from one form to another.
U1.5 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.18
Structured/Modular Programming
Facilitates parameter passing to communicate information. Controlled scope of data. Introduction of User defined data type. A rich set of control structure are able to further abstract the procedures. Employs Top Down approach in program design.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 5
Structured/Modular Programming
Advantages: Good for large programming projects. Supports modular proramming.
Bharati Vidyapeeths 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, time consuming & costly. Co-ordination among multiple programs is required for handling the changes made to mutually shared data items.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.19
Object = Data + Methods. 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]. Use Bottom-up program technique for program design.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.5 9
Advantages:
Increased programming productivity. Reusable Code. Decreased maintenance code. Greater speed. Lesser Complexities. Abstraction makes it possible to change the data structure of an object, without affecting the operation of the program.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.20
Disadvantages
Compiler Overhead. Runtime Overhead. Re-orientation of software developer to objectoriented thinking. Requires the mastery of software engineering and programming methodologies. Beneficial for large software projects in long run.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.21
If-then-else Rules Best suited for design of a knowledge base system. Example: MYCIN, XCON etc.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 5
Conclusion
The software crisis provided an impetus for the development of different programming paradigm to build more reliable and reusable system. 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, communication and imaging capabilities built into it.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.22
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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, reference, etc.)
U1.6 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
Learning Objectives
Overloaded procedures Default values to parameters inline procedures Overloaded operators Classes Exceptions Templates
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.6 9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.23
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.24
Comments in C++
Line comments // Avoid C style comments consider using the #if 0 until #endif pair of preprocessor directives #if 0
cout <<"xx2"; cout <<"xx3";
#endif
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 3
Beginning Comments
Heading Author Purpose Usage References File Formats Restrictions
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.25
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;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.7 8
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.26
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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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"; if (sizeof('x') == sizeof(int)) cout<<"int"; }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 0
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.e no argument
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 1
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.27
Declaration of Variables
C++ allows the declaration of a variable anywhere in the scope Permits initializaion of the variables at run time . float area = 3.14159*rad*rad; float average= sum/i;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 2
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 3
Data Types
Enumerated data type C defines the types of enums to be ints. C++ It is a separate data type enum Weekday { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; Weekday day = static_cast <Weekday>(4); day =8;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 4
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.28
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; char name[size]; 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 6
not actually a cast, request to the compiler to construct an (anonymous) variable of type typename from the expression expression.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 7
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.29
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.8 8
The `static_cast'-operator
used to convert one type to an acceptable other type. E.g., double to int.
d = static_cast<double>(a) / b;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,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; int *b = const_cast<int *>(&a); Also used to change data member of a class in const member function class sample { int a; public: void fn() const { const_cast<sample*>(this)->a = 10; } };
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 0
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.30
The `reinterpret_cast'-operator
used to reinterpret pointers
int a =25; int *b =reinterpret_cast<int *>a;
dangerous habit suppresses the normal type c ecking mechanism - h of the compiler
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 1
counter < 10; // local variable counter++) { cout<<::counter / counter); } return 0; } // global variable // divided by // local variable
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 2
Variable Aliases- 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;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 3
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.31
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 4
not bounded to a new memory location, but to the variables to which they are aliased Function in C++ take arguments passed by reference
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 5
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 6
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.32
Reference Parameters
Reference may be used in parameter declarations void inc_counter(int &counter){ ++counter;} For example: main(){ int a_count = 0; // Random counter inc_counter(a_count); cout << a_count << '\n'; return (0);}
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 7
#include <iostream.h> void swap1(int * a, int*b){ int temp = *a; *a = *b; *b = temp; return;} void swap2(int & a, int& b){ int temp = a; a = b; b = temp; return;}
void main(){ int i =5; int j = 10; cout<< i <<" "<<j<<"\n"; swap1(&i, &j); cout<< i <<" "<<j<<"\n"; swap2(i, j); cout<< i <<" "<<j<<"\n";}
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 8
Return by reference
An alias for the referred variable Used in operator overloading
void main(){ int a,b,c; #include <iostream.h> int & max(int & x, int & y) cout<<enter <a,b>:; { cin>>a>>b; if (x>y) max(a,b) = 425; return x; else cout<<a=<<a; return y; Cout<< b= <<b;} }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.9 9
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.33
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 00
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 01
Structs in C
/* definition of a struct PERSON_ */ typedef struct { char name[80]; char address[80]; } PERSON_; /* some functions to manipulate PERSON_ structs */ /* initialize fields with a name and address */ void initialize(PERSON_ *p, char const *nm, char const *adr); /* print information */ void print(PERSON_ const *p); /* etc.. */
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 02
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.34
};
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 03
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 04
C++ Strings
Bring in the string package using the statement:
#include <string>
Declaring a string
std::string my_name; // The name of the user
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 05
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.35
String Initialization
#include <string> using namespace std; int main() {
string stringOne("Hello World"); // using plain ascii-Z string stringTwo(stringOne); // using another string object string stringThree; // implicit initialization to "" }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 06
String Assignment
#include <string> using namespace std; int main() { string stringOne("Hello World"); string stringTwo; stringTwo = stringOne; // assign stringOne to stringTwo stringTwo = "Hello world"; // assign a C-string to StringTwo return 0; }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 07
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 08
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.36
String element
string stringOne("Hello World"); stringOne[6] = 'w'; // now "Hello world" if (stringOne[0] == 'H') stringOne[0] = 'h'; // now "hello world" stringTwo = "Hello World"; stringTwo.at(6) = stringOne.at(0); if (stringOne.at(0) == 'H') stringOne.at(0) = 'W'; return 0; }
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 09
More on Strings
Extract a substring:
result = str.substr(first, last); // 01234567890123 str = "This is a test"; sub = str.substr(5,6); // sub == 567890
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 10
Functions
A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program. A programmer can solve a simple problem in C++ with a single function but difficult and complex problems can be decomposed into sub-problems, each of which can be either coded directly or further decomposed. Decomposing difficult problems, until they are directly code-able as single C++ functions, is a software engineering method of stepwise refinement. These functions are combined into other functions and are ultimately used in main() to solve the original problem. In C++, a function must be defined before it can be used any where in the program.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 11
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.37
Functions
The general function definition is as given below:
datatype function-name (parameter list) { function body ; }
where datatype specifies the type of value the function returns (e.g.: int, char, float, double, user-defined). If no datatype is mentioned, the compiler assumes it as default integer type. The parameter list is also known as the arguments or signature of the function, which are the variables that are sent to the function to work on. If the parameter list is empty, the compiler assumes that the function does not take any arguments.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 12
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 13
Functions
The above program allows the user to input two integer values. It then lets the program compute the result by sending both the values to a function, by the name area(). 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. A function definition is automatically a declaration.
int area(int a, int b) ;
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 14
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.38
Default Arguments
C++ allows us to assign default values to function parameters, which are used in case a matching argument is not passed in the function call statement. The default values must to be specified at the time of definition.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 15
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 16
Pass-by-Value
A function can be invoked in two manners viz. pass-byvalue and pass-by-reference . The pass-by-value method copies the actual parameters into the formal parameters, that is, the function makes its own copy of the argument and then uses them. 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. All execution is done on the formal parameters; hence, it insures the safety of the data. The drawback of call-by-value is that a separate copy of the arguments is used by the function, which occupies some memory space thus increasing the size of the program.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 17
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.39
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 19
Call-By-Reference
The call by reference uses a different mechanism. In place of passing value to the function, which is called, a reference to the original variable is passed. A reference is an alias for the predefined variable. That is, the value of that variable can be accessed by using any of the two: the original or the reference variable name. When a function is called by reference, then the formal parameters become reference to the actual parameters in the calling function. This means that, in call by reference method, the called function does not create its own copy of the original values, rather, it refers to the original values only by different names. This called function works with the original data and any change in the value is reflected back to the data.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 20
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.40
Call-By-Reference
To write a function, which returns multiple output, we have to pass parameters by reference.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.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) ; void SumAverage (double, double, double &, double &) ;
The address (reference) of the actual variable is passed to the function, instead of its value. If the function changes the parameters value, the change is reflected back in the actual variables in the called function, since they share the same memory location.
U1.1 22
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 23
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.41
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 24
Call by Reference
In the above program, the function change() refers to the original value of orig, which is 10, by its reference a. The same memory location is referred to by orig in main() and by a in change(). Hence the change in a , by assigning value20, is reflected in orig also.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 25
Pass-by-value Vs Pass-by-reference
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 26
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.42
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 27
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 28
C++ Advantages
New programs would be developed in less time because old code can be reused. Creating and using new data types would be easier than in C. The memory management under C++ would be easier and more transparent. Programs would be less bug-prone, as C++ uses a stricter syntax and type checking. `Data hiding', the usage of data by one program part while other program parts cannot access the data, would be easier to implement with C++.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 29
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.43
Conclusion
New programs would be developed in less time because old code can be reused. Creating and using new data types would be easier in C++ than in C. The memory management under C++ would be easier and more transparent. Programs would be less bug-prone, as C++ uses a stricter syntax and type checking. `Data hiding', the usage of data by one program part while other program parts cannot access the data, would be easier to implement with C++.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 30
Summary
The C++ language evolved as a result of extensions and enhancements to C. It has efficient memory management techniques and a new style of program analysis and design that provides a foundation for data abstraction, encapsulation, inheritance, polymorphism, generic classes, exception handling, stream computation etc.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 31
Summary
OOP is generally useful for any kind of application but it is particularly suited for interactive computer graphics, simulations, databases, artificial intelligence, high performance computing and system programming applications.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 32
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.44
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 34
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.45
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.46
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 39
Recommended Books
TEXT: 1. A..R.Venugopal, Rajkumar, T. Ravishanker Mastering C++, TMH, 2009. 2. S. B. Lippman & J. Lajoie, C++ Primer, 6th Edition, Addison Wesley, 2006. REFERENCE: 1. R. Lafore, Object Oriented Programming using C++, Galgotia Publications, 2008. 2. D . Parasons, Object Oriented Programming with C++, BPB Publication. 3. Steven C. Lawlor, The Art of Programming Computer Science with C++, Vikas Publication. 4. Schildt Herbert, C++: The Complete Reference, 7th Ed., Tata McGraw Hill, 2008.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.1 40
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63,by Nitish Pathak
U1.47