C++
Derived from C Developed in 1983.
By: Bjarne Stroustrup
11-Nov-13 1
Origin of C++
C++ is an expanded version of C.The extension to C were first invented by Bjarne Stroustrup in 1980 at Bell Laboratories in Murray Hill,New Jersey. He initially called the new language C with Classes. However,in 1983 the name was changed to C++. Although C ,is one of the most liked and widely used professional languages in the world,the invention of C++ was necessiated by one major
11-Nov-13 2
Programming factor: Increasing complexity. C is an excellent programming language,it too has its limits.In C,once a program exceeds from 25,000 T0 100,000 lines of code,it become so complex that that is difficult to grasp as a totality.The purpose of C++ is to allow this barrier to be broken.The essence of C++ is to allow the programmer to comprehend and manage larger,more complex programs.
11-Nov-13 3
The object-oriented feature in C++,to use Stroustrups words, Allow programs to be structured for clarity,extensibility,and ease of maintenance without loss of efficiency. Although C++ was initially designed to aid in the management of very large programs ,it is in no way limited to this use. C++ shares Cs efficiency,much high-performance Systems software is constructed using C++.
11-Nov-13 4
OOPS
Object-oriented programming (OOP) is a new way of approaching the job of programming.Approaches to programming have changed dramatically since the invention of the computers,primarily to accommodate the increasing complexity of programs.When computers were first invented,programming was done by toggling in the binary machine instruction. The 1960 gave birth to structured programming.This is the method encouraged by languages such as pascal and C. The use of structured languages made it possible to write
11-Nov-13 5
Moderately complex programs fairly easily.However ,even using structured programming methods a project becomes uncontrollable once it reaches a certain size. Object-oriented programming has taken the best ideas of structured programming and combined them with several powerful new concepts that encourage to approach the task of programming in a new way. When programming in an object-oriented fashion,break down a problem into subgroups of
11-Nov-13 6
Related parts that take into account both code and data related to each group.Organize these subgroup into a hierarchical structure is called Objects. All Object-oriented programming languages have three traits in common. 1.Class 2.Objects 3.Encapsulation(Data Hiding)
11-Nov-13
11-Nov-13
CLASSES Classes are created using the Keyword Class.A class defines a new type that links code and data i.e. ADT(Abstract Data Type).This new type is then used to declare objects of that class. Thus,A class is a logical Abstraction,but an object has physical existence.
11-Nov-13
Tips
Access specifier: 1.Private 2.Public By default,Functions and data declared within a class are private to that class and may be accessed only by other member of the class.
11-Nov-13
10
Encapsulation
Encapsulation is the mechanism that binds together code and data,and that keeps both safe from outside interference or misuse. Encapsulation allows the creation of an object. Within an object,data may be private to the object and inaccessible to anything outside the object. An object provides a significant level of protection against some other,unrelated part of the program
11-Nov-13 11
Accidentally modifying or incorrectly using the private parts of the object.
11-Nov-13
12
Scope Resolution operator
The :: operator is used to link a class name with a member name in order to to tell the compiler what class the members belong to.However,the scope resolution operator has another related use,it can allow access to name in an enclosing scope that is hidden by a local declaration of the same name.
11-Nov-13
13
Static variable
Static variables are initialized to zero when the object is created.when declaring a static variable within a class,that is not allocating storage for it. Static variable provide a global definition.This is done by redeclaring the static variable using the scope resolution operator to identify which class it belongs to.This causes storage for the variable to be allocated.
11-Nov-13 14
When variables are declared as static,you are telling the compiler that one copy of that variable will exists and that all objects of the class will share that variable. Unlike regular data members,individual copies of a static member variable are not made for each object. No matter how many objects of a class are created, only one copy of a static data member exists.
11-Nov-13
15
Static function
Static function can have access to only to other static members(functions or variables) declared in the same class.A static member function can be called using the class name(instead of its object)
11-Nov-13
16
Inline functions
There is an important feature in C++,called an Inline function,that is commonly used with classes. Create short functions that are not actually called, rather their code is expanded inline at the point of each invocation.This process is similar to using a function like-macro . Inline functions are an important addition to C++ is that they allow to create very efficient code.Since
11-Nov-13 17
Classes typically require several frequently executed interface functions. Each time a function is called,a significant amount of overhead is generated by the calling and return mechanism.The trouble is that instruction take time time.However, when a function is expanded inline ,none of those operation occur.Although expanding function calls inline can produce faster run times. It is best to inline only very small functions.It is also a good idea to only inline those functions
11-Nov-13 18
That will have significant impact on the performance of the program.
11-Nov-13
19
Templates
A template function may be defined as unbounded set of overloaded functions.This means that all the possible parameters to the function are not known in advance and a copy of the function has to be created as and when necessary.Template function are declared using the keyword,template.Template are blueprints of a function that can be applied to different data types.The defination of the template begins with
11-Nov-13 20
The template keyword.This is followed by a commaseparated list of parameters types enclosed within the less than (<) and greater than (>).
11-Nov-13
21
Linked List
A data structure is a set of data,a structure variable,or an object of a class. A linked list is a chain of objects in which each objects consists of data and a pointer that stores the address (link) of the next logical object in the list.one way in which more than one object of the same data type can be stored in the memory is by using array. Arrays suffer from certain limitations.
11-Nov-13 22
1. Arrays are data structure of fixed size. 2.insertion and deletion of data involves re-shuffling of array elements. 3.Array manipulation is time-consuming and inefficient. The main advantage of linked list are: 1.It is not necessary to know the number of elments in advance,and allocate memory for linked lists. Memory is allocated whenever required. 2.inserting from linked list can be handled efficiently
11-Nov-13 23
Without having to restructure the lists. Type of Linked lists 1.Single linked list 2.Double linked list
3.Circular linked list
11-Nov-13
24
A single linked list is a list of objects to the same data type.Each object contains a pointer to the next object in the list.The list contains a pointer that points to the first object in the list.An object in a linked list is called a node.the last node in the list will have a pointer containing NULL.
Start
Info|Next
11-Nov-13
Info|Next
Info|Next
NULL
25
Each node has two parts. 1.The first part contains the information(the info part of the node).The info part may consists of variable. 2.The second part is a pointer,which contains the address of the subsequent node(The next part of the node) The start pointer stores the address of the first node in the list,thus keep tracking of the beginning of the list.
11-Nov-13
26
Circular Linked List
In a circular linked list,like the single linked list,start pointer point to the first node.the last node will not contain NULL,instead it will point to starting node.Circular linked list used by the operating system for file buffering.
Start Info|Next Info|Next
27
Info|Next
11-Nov-13
Double Linked List
The single linked list and the circular linked list are called one-way lists,since there is only one pointer, which points to the next node.Due to the presence of only one pointer which points to the next node.Due to this traversal in the reverse direction is not possible. However, in a double linked list traversal is possibe because two pointers prev,next.
11-Nov-13 28
Start
NULL
Prev|Info|Next
Prev|Info|Next
11-Nov-13
29
Operator overloading
Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. We can overload all the C++ operators except the following. 1.Class member access operators. 2.scope resolution operator. 3. Size operator(sizeof) 4.conditional operator.
11-Nov-13 30
Friend function may be used in the place of member function for overloading a binary operator.The only difference being that a friend function requires two argument to be explicitly passed to it while member function requires only one.
11-Nov-13
31
Rules for overloading operators
1.only existing operators can be overloaded.New operators cannot be created. 2.The overloaded operators must have at least one operand that is of user-defined type. 3.overloaded operators follow the syntax rules of the original operators.That cannot be overriden. 4. We cannot use friend functions to overload certain operators. However member functions can be used.
11-Nov-13 32
5. Unary operators take no explicit argument and return no explicit values. But, those overloaded by means of a friend function take one reference argument. 6. Binary operators overloaded through a member function take one explicit argument and those overloaded through friend function take two explicit arguments.
7. When using binary operators overloaded through member function, the left-hand operand must be 11-Nov-13 33 object of relevant class.
Rules for overloading operators
9. Binary arithmetic operators must explicitly return a value. They must not attempt to change their own arguments.
11-Nov-13
34
Stacks
Stack is a data structure in which operations like adding and removing are done from only one end, referred to as top. The stack is dynamic in nature, it constantly keeps changing its size due to the addition and deletion of data. A stack is also called a push-down list. Therefore stack is form of single linked list in which addition and deletion are performed in the beginning of the list.
11-Nov-13 35
Stacks
In a stack insert and delete items from the position level top. Adding item is known as push and deleting item is known as pop. This property is known as LIFO.
11-Nov-13
36
Queue
A queue is a data structure in which elements are deleted at one end, and elements are added at the other end. Queues are also called FIFO.
11-Nov-13
37
Register variable
The local variables that are used frequently in a program can be specified with the register. A register is a place for data storage in the microprocessor,if possible,the compiler will load the variable into a machine register,if it cannot,the remains in the RAM.This keyword is just a request,and not a demand, that the microprocessors register is used.
11-Nov-13 38
Register variables may increase the speed of a function if the variable selected are used frequently.
11-Nov-13
39
Recursion
A function can invoke another function.this is called function.whenever a function invokes itself,it is called recursion.All programming languages do not allow the use of recursive functions and procedure .Every time a recursive function calls itself,it does so with different values.Otherwise,it would enter an
11-Nov-13 40
Infinite loop.The function evaluates itself with the new arguments.The function then recursively calls itself with different arguments. Recursive procedure or function refers to itself,and satisfies the following two conditions. 1.There must be certain values,called base values,for which the function will not call itself. 2.Each time the function refers to itself,the argument passed muse be closer to its base value. n!=(n-1)!*n where n>1; and 0!=1
11-Nov-13 41
preprocessor
The various instructions to the compiler in the source code of a c or C++ program.These are called preprocessor directives,and although not actually part of the C or C++ language they expand the scope of C/C++ programming environment.
11-Nov-13 42
The preprocessor contains the following directives. #include #define #if #else #elif #define macro_name char-sequence
11-Nov-13
43
Binary Tree
Binary tree is a hieracchical representation of data structure. This is used for to reduce the searching of the data.The concept that binary tree follows is that of the binary searching where the set of data is divided into two parts and the data to be searched either in the first part or in the second part and so on.which reduces the speed by 1/8 that of the linear searching. 44 11-Nov-13
Binary tree ->In the binary tree maximum of two nodes can be attached with one node
Root
Left sub tree
Right subtree
11-Nov-13
45
Ordered binary tree->the difference of the height of two subtree can be up to one.
ROOT
11-Nov-13
46
// file1.cpp #include <iostream.h> int x=10,y=20; void f1(); void main() { cout<< " in the main app "; cout << x<< y; f1(); } 11-Nov-13
47
//file2.cpp #include <iostream.h> extern int x,y; void f1() { cout << " in the function"; cout<< x<<y; }
11-Nov-13
48
String Manipulations
1.String a(cdac delhi) cout<<a; output is cdac delhi 2.char str[21]=All decks Red alert; string a(str,11); cout<<a; output is All decks R 3.string b(str,28); cout<<b;
11-Nov-13 49
String manipulations
4. String a(5,c); cout<<a; output is: ccccc 5.String a(phaser set to stun); string b; b.assign(a,7,5); cout<<b; output is set t
6.string a; a.assign(Activate the shields,7); 11-Nov-13 cout<<a; output is: Activat
50