• COBOL, FORTRAN, C commonly known as the procedure oriented programming (POP).
• Problem is viewed as the sequence of things to be done.
• No. of functions are written to accomplish this task.
• Primary focus on functions.
• POP consists of writing a list of instructions for the computer to follow and organize the instructions into groups known as function.
• Less importance given to data (data undervalued).
• Many data items are placed as global so that they can be accessed number of functions.
• Data move openly around the system from function to function.
• It does not model real world problems very well.
Object- oriented programming paradigm
Object- oriented programming is an approach that provides a way of modularizing program by creating partitioned memory area both for data and functions that can be used as templates for creating copies of such modules on demand.
The main factor in the invention of OOP is to remove some of the flaws encountered in the procedural approach. OOP treats data as critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it, and protect it from accidental modification from out side functions.
Some of the feature of object oriented programming is:-
• Emphasis is in data rather than procedure.
• Data are tied closely to the functions that operate on it.
• Programs are divided into what are known as objects.
• Data is hidden and cannot be accessed by external functions.
• New data and function can be easily added whenever necessary.
• Objects communicate with each other through functions.
Basic concepts of object – oriented programming
• Object
• Classes
• Inheritance
• Reusability
• Creating new data types
• Polymorphism and over loading
• Data encapsulation
• Dynamic Binding
Objects:
• Objects are the basic run time entities in an object oriented system.
• Program is divided into objects rather than functions.
• Objects may represent any physical objects, user defined data types(e.g vector, time).
• It contains data and code to manipulate data.
• Objects are instance of classes.
• Example
Objects: Epson
Data: cost, price, speed, color
Functions: print.
Object: Student
Data
Name
Date-of-birth
Marks
…………….
Functions
Total
Average
Display
……….
Fig: Way to Represent Objects
Classes:
• Class models the behavior of an object.
• Object is an instance of class.
• Objects are variables of the type class.
• Once the class has been defined we can create any number of objects belonging to that class.
• Example
Class: printer
Object: dot matrices, inkjet laser.
Syntax to define class
Class class_name
{
Private:
Data members1;
Data members2;
[Member Functions]
Public:
Member Function
}
Eg.
Classs person
{
char name[30];
int age;
public:
void getdata();
void displaydata();
}
Inheritance:
• Ideas of class lead us to the idea of inheritance.
• Each sub – classes will share the common characteristics with the class from which it is derived.
• The origin class is the base class and the other classes which are derived from base class are derived class.
• Process by which objects of one class acquire the properties of object of another class known as inheritance.
Reusability:
• The concept of inheritance provides the idea of reusability.
• Once the class has been written, created and debugged, it can be distributed to other programmers for use in the own programs. This is called reusability.
• We can add new features to the existing class without modifying.
• A derived class inherits all the feature of base class and in derived class we can add new feature.
Creating a new data type:
• We can create a new data types with the help of objects.
• Suppose in order to work with 2 dimensional position (x, y positions)
• We can create a class that incorporates these to variables.
• We can then create objects pos1, pos2, pos3.
• We can do the operations on the positional values.
pos3=pos1+pos2
Polymorphism and Overloading:
• One thing with several different form is polymorphism
• Using operators or functions in different way depending upon what they are operating on helps us to achieve polymorphism.
• Overloading is a kind of polymorphism.
• Example: Overloading->operator overloading-function overloading.
Example:
Shape
Draw()
Triangle
Draw(Tri)
Circle Obj
Draw(circle)
Box Obj
Draw(box)
Data encapsulation:
• The wrapping of data and function into one single unit (class) is known as encapsulation.
• Only the functions within the class can access the data.
• The data is not accessible to outside world.
• This insulation of the data from direct access by the program is called data hiding.
Dynamic Binding:
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic Binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time.
It is associated with polymorphism and inheritance.
Shape
Draw()
Triangle
Draw(Tri)
Circle Obj
Draw(circle)
Box Obj
Draw(box)
• By inheritance, every object will have this procedure.
• Its algorithm is, however unique to each object and so the draw procedure will be redefined in each class that defines the object.
• At run time, the code matching the object under current reference will be called.
Benefit of OOP:
• Through inheritance, we can eliminate redundant code and extend the use of existing classes.
• The principle of data hiding helps the programmer to build secure programs.
• It is easy to partition the work in a project based on objects.
• We can build programs from the standard working modules, thus saving time and giving more productivity.
• Software complexity can be easily managed.
• Object-oriented systems can be easily upgraded from small to large systems.
Application of OOP:
• Most important application of OOP has been in the use in the interface design.
• The other areas can be
• Simulation and modeling.
• Object oriented database.
• Real time System.
• Hypertext, Hypermedia.
• Artificial Intelligence and expert system.
• Neural Networks and parallel programming.
• Decision supports and office automation system.
Chapter 2: C++ language basic syntax
Input and Output :
Input with cin:
cin>>number1;
The identifier cin ( pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard. The >> is the extraction or get from operator. It takes from the stream object on its left and places it in the variable on its right.
Object Extraction Operator Variable
Fig: Input with cin
Output with cout:
cout<<”Hello Class”; The identifier cout(pronounced as “C out”) is actually an object. It is predefined in C++ to correspond to the standard output stream. A stream is an abstraction that refers to flow of data. The standard output stream normally flows to the display. The operator << is called the insertion or put to operator. It directs the contents of the variable on its right to the object on its left. In First it directs the string constant “Hello Class” to cout, which sends it to the display. Object Insertion operator Variable Fig: output with cout Example: //simple c++ program #include
void main()
{
int f;
cout<< “Enter the temperature in Fahrenheit:”; cin >> f;
int c = (f - 32 ) * 5 / 9;
cout <<”Equivalent in Celsius is: ”<< c <<”\n”; } Output: Enter temperature in Fahrenheit: 212 Equivalent in Celsius is: 100 Derived data types: Arrays The application of arrays in C++ is similar to that in C. The only exception is the way that character arrays are initialized. When initializing a character array in ANSI C, the compiler will allow us to declare the array size as the exact length of the string constant. For instance, char string [3] =”xyz”; is valid in ANSI C. It assumes that the programmer intend to leave out the null character \0 in the definition. But in C++, the size should be one larger than the number of characters in the string. char string [4] = “xyz”; //o.k. for C++ Functions There has been some modification and implements which will be discussed later. Pointers Pointers are declared and initialized as in C. Example: int *p; //int pointer p=&x; // address of x assigned to p *p=5; //5 assigned to x through indirection C++ adds the concept of constant pointer and pointer to a constant. char *p const ptr1=”GOOD”; //constant pointer We cannot modify the address that ptr1 is initialized to. int const *ptr2=&m; //pointer to a constant. ptr2 is declared as pointer to a constant. It can point to any variable of correct type, but the contents of what it points to cannot be changed. We can also declare both the pointer and the variable as constants in the following way: Const char *const cp=”xyz”; This statement declares cp as a constant pointer to string which has been declared as a constant. In this case, neither the address assigned to the pointer cp nor the contents it points to can be changed. Pointers are extensively used in C++ for memory management and achieving polymorphism. Standard Conversion Implicit Conversion We can mix data types in expressions. For example, m=5+2.5; is a valid statement. Whenever data types are mixed in an expression, C++ performs the conversion automatically. This process is known as implicit or automatic conversion. Integral widening conversion Whenever a char or short int appears in an expression, it is converted to an int. This is called integral widening conversion. The implicit conversion is applied only after completing all integral widening conversions. New and Delete operator New operator • Reserves a memory and returns a pointer to its staring point. • New operator is used for dynamic allocation of memory and can be used instead of malloc(). char *p; *p = new char [5]; //set aside memory for 5 character ----------------------------------------- int *p, size; cin>>size;
*p= new int [size];
------------------------------------------
ptr= new int [size];
ptr= pointer variable
new = keyword
Delete operator
• Delete operator returns memory to the operating system.
delete ptr;
Returns to the systems whatever memory was pointed to by ptr.
Manipulators
• Manipulators are the operators used to format the display of data.
• Operators used with the << (insertion operator) to modify the way data is displayed. • iomanip.h header file needed. endl manipulator • It causes a line feed to be inserted into the output stream • Same as a single ‘\n’ character. cout<< endl; cout<< “hello world”<
enum days { sun, mon, tue, wed, thurs, fri, sat} ;
void main()
{
days day1,day2;
day1=mon;
day2=tue;
cout<
#incude
#incude
void add();
void add(int,int);
void add(int,int,int);
void main()
{
int a=5,b=5,c=5;
add();
add(a,b);
add(a,b,c);
getch();
}
void add()
{
int a=5,b=5;
cout<<(a+b)<
#include
#include
void show();
void show (char);
void show (char, int);
void main()
{
show();
show(“*”);
show(“*”,5);
getch();
}
void show()
{
for(int i=0;i<5;i++) cout<<’*’; cout<
show(‘*’);
show(2);
show(a);
show(num);
getch();
}
void show(char c)
{
for(int i=0;i<5;i++) cout<
#include
//function defined before the call is made
inline int add(int a,int b)
{
return (a+b);
}
void main()
{
int a,b;
cout<<”Enter the value for a and b:”; cin>>a>>b;
cout<<”the added value is:”; cout<
#include
void area (int l=2, int b=3); //prototype with default argument.
void main()
{
area();
area(3);
area(5,5);
getch();
}
void area(int l, int b)
{
cout<<”the area is:”<
• Default argument follows an equal sign.
• If one argument is missing, it is assumed to be the last argument.
Above where area(3);
l=3, and b is assumed to missing argument so default value b=3 is applied.
Default arguments are useful
• If the arguments are almost always have the same value so no need to write again and again.
• The programmer can increase the capability of a function.
No comments:
Post a Comment