OOP with C++ Basic Concepts of Object Oriented Programming: 1. Objects: Objects are the basic run-time entities in an object oriented system. It may represent a person, a place, a bank account, vector, time or any list item that the program has to handle. 2. Class: A class is a collection of objects of similar type. Example: Mango, apple and grapes are members of the class fruit. If fruit has been defined as class then fruit mango; //will create an object mango belonging to class fruit Objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class. Classes are user-defined data types and behave like the built-in types of programming language. 3. Data abstraction and Encapsulation: The wrapping up of data and functions into single unit is known as encapsulation. This concept binds together the data and functions that manipulate the data and functions that manipulate the data and that keep both safe from outside interference and misuse. The insulation of the data from direct access by the program is called data hiding or information hiding. Abstraction refers to the act of representing essential features without including the background details or explanations. Classes defined as a list of abstract attributes and function to operate on these attributes without presenting the details. The attributes hold information so they are called data members and the functions operate on these data are called member functions. Since classes use the concept of data abstraction, they are known as Abstract Data Types (ADT). 4. Inheritance: Inheritance I the process by which objects of one class acquire the properties of objects of another class. The concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. 5. Polymorphism: Means ability to take more than one form. 6. Dynamic Binding: Binding means connecting the function call to function implementation. Compiler should match function calls with the correct definition at the run time this is called dynamic binding or late binding. The code associated with the function is not known until the program is executed. 7. Message Passing: Message passing is sending and receiving of information by the objects same as people exchange information. Message passing involves specifying the name of objects, name of function and the information to be sent. Applications of OOP: 1. 2. 3. 4. 5. User interface design such as windows, menu Real time systems Object oriented database Neural networks and parallel programming Decision support and office automation system Benefits of OOP: 1. It is easy to model real world system as real objects are represented by programming objects in OOP. The objects are processed by their member data and functions. It is easy to analyze the user requirements. 2. With the help of inheritance, we can reuse the existing class to derive a new class such that the redundant code is eliminated and the use of existing class is extended. This saves time and cost. 3. In OOP, data can be make private to a class such that only member functions of the class can access the data. This principle of data hiding helps the programmer to buils a secure program. 4. With polymorphism, the same function or same operator can be used for different purpose. 5. Large problems can be reduced to smaller and more manageable problems. Easy to partition the work in a project based on objects 6. It is possible to have multiple instances of class. Each object has its own separate member data and function. Introduction of C++: C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented and generic programming. C++ is regarded as a middle-level language, as it comprises a combination of both highlevel and low-level language features. C++ was developed by Bjarne Stroustrup starting in 1980 at Bell Labs in USA, as an enhancement to the C language and originally named C with Classes but later it was renamed C++ in 1983. C++ is a superset of C. Therefore almost all C programs are also C++ programs. The Most important facilities that C++ adds on to C are classes, inheritance, function overloading and Operator overloading. These features enable creating of abstract data types, inherit properties from existing data types and support polymorphism, so C++ is known as truly object oriented language. Tokens: The smallest individual units in a program are known as tokens. C++ has the following tokens: 1. 2. 3. 4. 5. Keywords Identifiers Constants String Operators Keywords: Keywords are reserved identifier and cannot be used as names for the program variables or other user-defined elements. Identifiers: Identifiers refers to the names of variables, functions, arrays, classes, etc. created by programmer. The Rules for naming the identifiers are as follows: 1. Only alphabetic characters, digits and underscores are permitted. 2. The name cannot start with a digit. 3. Uppercase and lowercase letters are distinct. 4. A keyword cannot be used as variable name. Constant: Constants refers to fixed values that do don’t change during the execution of a program. C++ Data Types can be classified into three categories as follows: 1. User-defined type structure union class enumeration 2. Built-in type int char void float double 3. Derived type array function pointer reference Several of the types can be modified using one or more modifiers signed, unsigned, short, long. An enumerated data type is user-defined type which provides a way for attaching names to numbers. Enumerated Data Type: Enumerations are declared via the enum keyword. Each enumerator is automatically assigned an integer value based on its position in the enumeration list. By default, the first enumerator is assigned with 0 and second is assigned with 1 and so on. It is possible to define the value of enumerator explicitly. These values can be positive, negative. Each subsequent enumerator has a value one greater than previous enumerator. An enumerated data type (also called Enumeration) is a data type where every possible value is defines as a symbolic constant. Dynamic Initialization of variables: Initialization of variables at runtime refers to Dynamic Initialization. float area=3.14*r*r; Both the declaration and the initialization of a variable can be done simultaneously at the place where the variable is used for the first time. Reference Variables: A reference variable is an alias that is another nme for already existing variable. Once a reference is initialized with a variable, either the variable name or reference name may be used to refer to the variable. Data-type & reference-variable= variable-name Example: int no=70; int & result=no; no is a int type variable that has already been declared, result is the alternative name declared to represent the variable no. Both the variables refer to the same data object in the memory. A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it names. A major application of reference variable is in passing arguments to functions. Such functions are known as call by reference. Void fact(int &x) // uses reference Operators in C++: << >> :: ::* ->* .* new delete Insertion operator Extraction operator Scope Resolution operator Pointer to member declarator Pointer to member operator Pointer to member operator Memory allocation operator Memory release operator Scope resolution operator: This operator is used to uncover a hidden variable. This operator allows access to the global version of variable. A major application of scope resolution operator is in the classes to identify the class to which a member function belongs. Memory Management Operator: C++ defines two unary operators new and delete that perform the task of allocating and freeing memory in better and easier way. These operators manipulate memory on the free store therefore they are known as free store operator. A data object can be created by using new and destroyed by using delete, as and when required. A data object created inside a block with new, will remain in existence until it is destroyed by using delete. Pointer-variable=new data-type; Pointer-variable is pointer of type data-type. The new operator allocates sufficient memory to hold a data object of type data-type and returns the address of the object. The pointer variable holds the address of the memory space allocated. We can also initialize the memory using the new operator int *p=new int(25); new can be used to create a memory space for any data type including user-defined types such as arrays, structures and classes. int *p=new int[15]; create memory space for an array of 15 integers. p[0] will refer to the first element, p[1] to the second element and so on. When data object is no longer needed, it is destroyed to release the memory space for reuse. delete pointer-variable; delete [size] pointer-variable; The size specifies the number of elements in the array to be freed. The new operator offers the following advantages over the function malloc() as follows: 1. It automatically computes the size of the data object. We need not use the operator sizeof. 2. It automatically returns the correct pointer type, so that there is no need to use a type cast. 3. It is possible to initialize the object while creating the memory space. 4. Like any other operator, new and delete can be overloaded. Manipulator: Manipulators are operators that are used to format the data display. The most commonly used manipulators are endl and setw. endl is the line feed operator in c++. It acts as a stream manipulator whose purpose is to feed the whole line and then point the cursor to the beginning of the next line.\n can be used instead of endl for the same purpose. Setw manipulator sets field width. It sets the no of characters to be used as the field width for the next insertion operation. To use manipulators must include <iomanip>header file. Cout<<setw(10)<<”SYBCA”; Type cast operator: Type cast is a special operator that forces one data type to be converted into another. It is unary operator and has same precedence as any other unary operator. (type)expression; where type is the desired data type. Exapmle: Average=sum/float(i); Expressions: An expression is a combination of operators, constants and variables arranged as per the rules of language. It may also include function calls which return values. An expression may consist of one or more operands, and zero or more operators to produce a value. Expressions may be of the following seven types: Constant Expressions: Constant expressions consist of only constant values. Examples: 75, 80 + 5 /3, ‘s’. Integral Expressions: Integral Expressions are those which produce integer results after implementing all the automatic and explicit type conversions. Example: a, a * b – 3, 7 + int(3.7) //a and b are integer variables Float Expression: Float Expressions are those which, after all conversions, produce floating point results. Example: p + q, p * q /10, 7 + float(10) //p and q are floating-point variables Pointer Expressions: Pointer Expressions produce address values. Example: &n, ptr, ptr + 1 //m is variable and ptr is a pointer Relational Expressions: Relational Expressions generate results of type bool which takes a value true or false. Example: x <= y, a+b == p+q, a+b > 100 When arithmetic expressions are used on either side of relational operator, they will be evaluated first and then results compared. Relational Expressions are also known as Boolean Expressions. Logical Expressions: It Combine two or more relational expressions and produces bool type results. Example: a>b && x==5, x==1 || y==2 Bitwise Expressions: These Expressions are used to manipulate data at bit level. They are basically used for testing of shifting bits. Example: a <<3 b >>2 //shift three bit position to left //shift two bit position to right Special Assignment Expressions: Chained Assignment: x = y = z = 10 //first 10 is assigned to z then y and at last to x. A chained statement cannot be used to initialize variables at the time of declaration. Embedded Assignment: x = (y=100) + 75 //first 100 value assigned to y and then result 100+75 is assigned to x. Compound Assignment: Compound assignment operator is combination of the assignment operator with binary arithmetic operator. X += 10; It is also known as short-hand assignment operator. Control Structures: 1. Sequence structure (straight line) 2. Selection structure (branching) 3. Loop structure (iteration or repetition)