You are on page 1of 18

Brief History of C++

1. 2. During 1970 Dennis Ritchie created C Programming language. In the early 1980s, also at Bell Laboratories, another programming language was created which was based upon the C language. 3. New language was developed by Bjarne Stroustrup and was calledC+ +. 4. Stroustrup states that the purpose of C++ is to make writing good programs easier and more pleasant for the individual programmer. 5. C++ programming language is extension to C Language. 6. In C we have already used increment operator (++) . Therefor we called C++ as Incremented C means Extension to C.

Characteristics of c++:
C++ Provides huge Function Library thats why its popularity is increasing day by day and more programmers are inclining towards C++ due to its multiple features 1. 2. 3. 4. C++ is an Object Oriented Programming Language (OOPL). C++ have huge Function Library C++ is highly Flexible language with Versatility. C++ can be used for developing System Software viz., operating systems, compilers, editors and data bases. 5. C++ is suitable for Development of Reusable Software. , thus reduces cost of software development. 6. C++ is a Machine Independent Language.

How do Java and C# create cross-platform portable programs, and why cant C++ ?

Consider Scenario of C++ Language


Portability of any language depends on the object code created by compiler. 2. C++ Compiler produces machine code which is directly executed by the CPU. 3. C++ Code is thus machine dependent and tied to particular Operating System (OS). 4. If we try to execute C++ program on the another Operating C++ thenwe must recompile program before executing program.
1.

Consider Scenario of Java Programming Language


Java is created by Sun Micro System. 2. Java Compiler produces Intermediate code called Byte Code. 3. Byte Code i.e intermediate code is executed by the Run Time Environment. 4. In Java Run Time Environment is called as JVM [ Java Virtual Machine] 5. If we have JVM already installed on any platform then JVM can produce machine dependent Code based on the intermediate code.
1.

Consider Scenario of C# Programming Language


1.
2.

C# was created by Microsoft.

C# compiler produces intermediate code called as MSIL. (MicroSoftIntermediate Language). 3. MSIL i.e intermediate code is executed by CLR (Common Language Run Time) 4. If we have CLR already implemented on any platform then CLR can produce machine dependent Code based on the intermediate code.

Pillars of Object Oriented Programming


The features which make the language Object Oriented Language, are called the Pillars of Object Oriented Programming.

Pillars Of Object Oriented Programming

Major Pillars
1 Abstraction

Getting only essential things and hiding unnecessary details is called abstraction. It is the public access region of the class. Generally, it declares the operations that can be invoked by the clients on object of class. Avoid using data members in public region of class, as it may be directly changes by the client.

Example of Abstraction in c++

2 Encapsulation

Binding of data and code together is called encapsulation. Encapsulation maintains integrity of object. For example, the Man class has a walk() method. The code for the Walk() method defines exactly how a walk happens. The encapsulation is most often achieved through information hiding. Information hiding is the process of hiding all the secrets of an object that do not contribute to its essential characteristics; typically,structure of an object is hidden, as well as implementation of its methods. The act of portioning a program into individual components can reduce its complexity to some degree. Modularity can be physical modularity(e.g. divide program into .h, .cpp, .rc files) or logical modularity(e.g. namespace) Modularization consists of dividing program into modules that can be compiled seperately, but which are interconnected. Modularity helps in easier maintenance of a complex software.

3 Modularity

4 Hierarchy
Hierarchy is ranking or ordering of abstractions. Main purpose of hierarchy is to achieve re-usability. The hierarchies are: 1. Inheritance (is-a relationship): Acquiring all the properties(data members) and behaviors of one class by another class is called inheritance.

Class Diagram-Inheritance

2. Composition (has-a relationship): When object is madhe from other small objects it is called composition.

Class Diagram-Composition

A is owner of B and B is non shareable. 3. Aggregation (has-a relationship):

Class Diagram-Aggregation

A has B, A is owner of B, and B is shareable. 4. Aggregation (work-for relationship): Two objects works together for long time. It is bidirectional relationship

In association, both are independent entities but just working together for long duration.

Class Diagram-Association

5. Dependency (use-a relationship): Sometimes the relationship between two classes is very weak.

They are not implemented with member variables at all. Rather they might be implemented as member function arguments.

Class Diagram-Dependency

Polymorphism
It is Greek word which is a combination of poly(many) + morphism(forms/behavior). One interface having many behaviors such phenomenon is called polymorphism. Types of polymorphism: 1. Compile time polymorphism: It is also called as static polymorphism, false polymorphism, early binding.

When call to functions is resolved at compile time it is called compile time polymorphism. In c++, Compile time polymorphism is achieved by using function overloading and operator overloading.

2. Run time polymorphism: It is also called as dynamic polymorphism, true polymorphism or late binding.

When call to the function resolved at run time, it is called run time polymorphism. In c++, run time polymorphism is achieved by using function overriding.

Minor Pillars
1 Concurrency

The concurrency problem arises when multiple threads simultaneously access same object. We may use a class library for providing threading support.

We may use interrupts to give illusion of concurrency which need hardware details. You need to take care of object synchronization when concurrency is introduced in the system. It is property by which object maintains its state across time and space. The concept gives rise to the concept of object oriented database. It talks about concept of serialization and also about transferring object across network.

2 Persistence

Class and Object

Class and Object in c++

Class:

It is user defined data type combining data members and member functions. We can restrict the access of the member using access specifiers . (public, private, protected) By default every member in class is private.

Object:

It is the variable of class. Object gets physical memory and contains data members which are declared into the class.

Example:
class Sample { private: int a; int b; public: void function(); }; // class declaration // access specifier // data members // member function

int main() { Sample s1; // s1 is an object of class Sample //----- code---return 0; }

Characteristics of an object:
1. State:

State of an object is determined by the values of data members of that object. If two objects have same value of data members, then the two objects are having same state.

2. Behavior:

A member function in the class decides behavior of the object. Two objects having different states will show different behavior by calling the same member function.

3. Identity:

Every object has unique address.

Data members:

Data members are declared with any of the fundamental types, also with pointers, reference, array types and user-defined types. By default every data member in class is private. Initializers are not allowed inside the class definition.

Member functions:
1. Constructor:

It is a member function of the class having same name as that of class. It doesnt have any return type. (Not even void) Constructor is automatically called only once in a lifetime of an object when it is created. Constructor initializes data members of the class. If we dont write any constructor, compiler provides default constructor for the class. Constructor can be overloaded.

class Sample { private: int a; public: Sample(); ~Sample(); }; int main() { Sample s1; //constructor is called //----- code---return 0; } //constructor //destructor

2. Destructor:

It is a member function of the class having same name as that of class preceded by ~. It doesnt have any return type. (Not even void) Destructor is automatically called when object goes out of scope. Destructor de-initializes data members of the class. If we dont write any destructor, compiler provides default destructor for the class. Destructor can NOT be overloaded.

Mutators:

Mutators are the member functions of the class, which modify state of the object. These functions modify values of the data members. These are typically setter functions in the class.

class Sample { private: int a; public: Sample(); ~Sample(); int GetA(); void SetA(int); void Display(); }; //constructor //destructor //accessers //mutators //Facilitators

Accessers:

Inspectors are the member functions of the class, which read the state of the object. These are typically getter functions in the class

Facilitators:

Facilitators are the member functions of the class, which do not directly contribute to the operation of that class.

This function provides additional functionality like scanning or printing object data.

What is Object Oriented Language?


The programming methodology to organize complex program into simple programs by using concepts such as Abstraction, Encapsulation,Polymorphism and Inheritance are called Object Oriented Programming languages.

Why c++ is Semi Object Oriented Language?


1. 2. 3. Main function is outside the class. Friend function is present which can access private data of other class. Global variables allowed.

we have elaborated above points below -

Case 1 : Main Function is Outside Class


Semi Object Oriented : Live Example
#include<iostream.h> Class Person { ------------} void main() { Person P1; ------------------------}

Above C++ Code Snippet will start its execution from main funtion. Inside Main Function we are going to create Object of the Class. Creation of Class/Object is optional while the creation of main function is mandatory . We can say that C++ is close to C (structural) and C++ provides optional OOP features , thus we can say that it is semi object oriented.

Pure Object Oriented Program : Live Example


public class HelloWorldExample{ public static void main(String args[]){ System.out.println("Hello World !"); } }

It is Java Code which clearly shows that We have to create Class. Still it tells us that main is going to execute first as it is Static.

Case 2 : Global Variables


In C++ we can declare global variables that are accessible in all classes and have life time equal to life time of entire program. 2. However in Java (Complete OOP) we have to declare variables inside Class only , We can use access specifier to make them accessible outside the class.
1.

Advantages of Object Oriented Programming :1. 2. 3. 4. Exploits the expressive power of all object oriented programming languages. Encourages the resuse of software components. Leads to the systems that are more resilient to change. Reduces the development risk.

Features of Object Oriented Programming :1. More focus on data.

2. 3. 4. 5. 6.

Bottom-up approach. Abstraction Encapsulation Inheritance Polymorphism

What is Namespaces in C++ Programming Language:


Using namespace we can have class,global variables,functions under one name. We can change global scope to Sub-scope.

Namespace in c++

Namespace is a logical compartment used to avoid naming collisions. The naming conflict or Collision always Occurs may occur When same program is using more than one library with some variables or functions having same name. The name collision may occur for global variables, global functions, classes etc.

Default namespace is global namespace and can access global data and functions by proceeding (::) operator. We can create our own namespace. And anything declared within namespace has scope limited to namespace.

Creating a namespace:

Creation of namespace is similar to creation of class. Namespace declarations appear only at global scope. Namespace declarations can be nested within another namespace. Namespace declarations dont have access specifiers. (Public or private) No need to give semicolon after the closing brace of definition of namespace. We can split the definition of namespace over several units.

Syntax:
namespace namespace_name { //member declarations }

Live Example :
//In firstHeader.h namespace ns1 { class one { //----Declarations--}; class two { //----Declarations--}; } //In secondHeader.h

namespace ns1 // can continue the defination of ns1 over multiple header files { class three { //----Declarations--}; }

What is Unnamed Namespace ?


Unnamed namespaces are the replacement for the static declaration of variables. They are directly usable in the same program and are used for declaring unique identifiers. In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler. The unnamed namespaces you have created will only be accessible within the file you created it in.

#include<iostream> using namespace std; namespace { int i; } int main() { i=10; cout<< "Value : "<<i; return 0; }

Output:
Value : 10

How to Use Namespaces in C++ Programming Language :

Following are the ways to refer to namespace members:

Way 1 : Using Scope Resolution :


In this method, we use the namespace name, scope resolution operator (::) and member of that namespace.

Way 2 : Using Directive:


We can use using directive to specify the namespace.
#include<iostream> using namespace std; namespace first { int i; } namespace second { int i; } int main() { first::i=1; second::i=2;

//scope resolution

using first::i; //using directive cout<<i; }

Way 3 : Using declaration:


It is a declaration within the current scope. This means it can override names from a using directive.
#include<iostream> using namespace std; namespace first { int i; }

using namespace first;// using declaration int main() { i=2; cout<<"Value : "<<i<<endl; }

Output:
Value : 2

Different Verities of Using Namespace :


1.We can Have Class Definition inside Namespace
#include <iostream> using namespace std; namespace MyNameSpace { int num1; int num2; class Student { int marks; public: Student() { marks = num1; } }; } int main() { MyNameSpace::num1 = 70; MyNameSpace::num2 = 90; MyNameSpace::Student ob1(); }

num1,num2 are two variables under same namespace name MyNamespace. We can Initialize them in main function by using Scope Resolution Operator.

We can Declare Class inside Namespace and thus we can have multiple classes with same name but they must be in different namespace.

2.We can Have Same Class Definition inside different Namespace


#include <iostream> using namespace std; namespace MyNameSpace1 { int num1; int num2; class Student { int marks; public: Student() { marks = num1; } }; } namespace MyNameSpace2 { int num1; int num2; class Student { int marks; public: Student() { marks = num1; } }; } int main() { MyNameSpace1::num1 = 80; MyNameSpace1::num2 = 50; MyNameSpace1::Student ob1(); }

You might also like