Professional Documents
Culture Documents
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++ ?
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.
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.
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
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:
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
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:
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.
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.
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.
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.
2. 3. 4. 5. 6.
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--}; }
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
//scope resolution
using namespace first;// using declaration int main() { i=2; cout<<"Value : "<<i<<endl; }
Output:
Value : 2
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.