
Object Oriented Programming:-Object-oriented programming (OOP) is a software programming model constructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods). OOP features include the following:- • Encapsulation: This makes the program structure easier to manage because each object’s implementation and state are hidden behind well-defined boundaries. • Polymorphism: This means abstract entities are implemented in multiple ways. • Inheritance: This refers to the hierarchical arrangement of implementation fragments. Object-oriented programming allows for simplified programming. Its benefits include reusability, refactoring, extensibility, maintenance and efficiency. Introduction to Classes and Objects ❖ Class:-The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. The variables inside class definition are called as data members and the functions are called member functions. A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows − class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section. ❖ More about Classes:- 1. Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section). 2. Class's member functions can be defined inside the class definition or outside the class definition. 3. Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public. S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 1 4. All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc. 5. Objects of class holds separate copies of data members. We can create as many objects of a class as we need. 6. Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later. ❖ Object:- A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box − Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Both of the objects Box1 and Box2 will have their own copy of data members. ❖ Classes and Objects in Detail:- So far, you have got very basic idea about C++ Classes and Objects. There are further interesting concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed below − Sr.No Concept & Description 1 Class Member Functions A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. 2 Class Access Modifiers A class member can be defined as public, private or protected. By default members would be assumed as private. 3 Constructor & Destructor A class constructor is a special function in a class that is called when a new object of the class is created. A destructor is also a special function which is called when created object is deleted. 4 Copy Constructor The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 2 5 Friend Functions A friend function is permitted full access to private and protected members of a class. 6 Inline Functions With an inline function, the compiler tries to expand the code in the body of the function in place of a call to the function. 7 this Pointer Every object has a special pointer this which points to the object itself. 8 Pointer to C++ Classes A pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is really just a structure with functions in it. 9 Static Members of a Class Both data members and function members of a class can be declared as static. Difference Between C and C++ :- C C++ C was developed by Dennis Ritchie between C++ was developed by BjarneStroustrup in 1979 1969 and 1973 at AT&T Bell Labs. with C++'s predecessor "C with Classes". When compared to C++, C is a subset of C++ is a superset of C. C++ can run most of C code C++. while C cannot run C++ code. C supports procedural programming C++ supports both procedural and object oriented paradigm for code development. programming paradigms; therefore C++ is also called a hybrid language. C does not support object oriented Being an object oriented programming language programming; therefore it has no support C++ supports polymorphism, encapsulation, and for polymorphism, encapsulation, and inheritance. inheritance. In C (because it is a procedural In C++ (when it is used as object oriented S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 3 programming language), data and programming language), data and functions are functions are separate and free entities. encapsulated together in form of an object. For creating objects class provides a blueprint of structure of the object. In C, data are free entities and can be In C++, Encapsulation hides the data to ensure manipulated by outside code. This is that data structures and operators are used as because C does not support information intended. hiding. C, being a procedural programming, it is a While, C++, being an object oriented function driven language. programming, it is an object driven language. C does not support function and operator C++ supports both function and operator overloading. overloading. C does not allow functions to be defined In C++, functions can be used inside a structure. inside structures. C uses functions for input/output. For C++ uses objects for input output. For example scanf and printf. example cin and cout. C does not support reference variables. C++ supports reference variables. C has no support for virtual and friend C++ supports virtual and friend functions. functions. C provides malloc() and calloc()functions C++ provides new operator for memory allocation for dynamic memory allocation, and delete operator for memory de-allocation. and free() for memory de-allocation. C does not provide direct support for error C++ provides support for exception handling. handling (also called exception handling) Exceptions are used for "hard" errors that make the code incorrect. ******* S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 4 ➢ Data Type in C++:- Data Type Basic/Primitive/Primary Data type Derived Data Type User Defined Data Type \ Array Pointer Function Reference Structure Union enum class Integer Float Void Char Boolean ➢ Reference variable (&):- It is a new type of variable introduced in c++. A reference variable provides an alternative name for a previously defined variable. Syntax:- Datatype&reference_name=variable_name; Ex:-int a=10; Int& b=a; Ex:- Void swap(int&, int&); //function declaration swap(a,b); //function calling void swap(int&p1, int&p2) //function definition { P2=p1+p2; P1=p2-p1; P2=p2-p1; } ➢ Dynamic initialization of variable:-In c++ a variable can be initialized at run time using expression at the place of declaration. Such type of initialization of variable Is known as dynamic initialization of variable. In C a variable must be initialized using a constant expression and C compiler would fix the initialization code at the time of compilation. S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 5 ➢ Operator in C++:- All C- operators are valid in C++. In addition c++ provides some new operators:- (i) << :- Insertion operator (put to) (ii) >> :- Extraction operator (get from) (iii) :: :- Scope resolution (iv) ::* :- pointer to member operator (v) ->* :- pointer to member operator (vi) .* :- pointer to member operator (vii) new :- memory allocation operator (viii) delete :- memory release operator (ix) endl :- line feed operator (x) setw :- filled width operator cout<<”Sum of”<<a<<”and”<<b<<”is”<<c; cout<<a<<”+”<<b<<”=”<<c; cin>> inta,b; cin>>a>>b; ➢ Manipulators in C++:- Manipulators are operators that are used to format the data displayed. They are:- (i) Setw:- a=10,b=20 Cout<<setw(4)<<a<<b; 10 20 (ii) endl(new line):- cout<<”a”<<a<<endl<<”b=”<<b; (iii) setprecision:- To set floating point a=12.75678 printf(%2f”,a); Output:-12.76 Cout<<setprecision(2)<<a; Output:- 12.76 (iv) setfill:- this manipulator always used with the combination of ‘setw’ manipulator to fill with specified character in preceded blank space. Ex:-int a=10; Cout<<setw(4)<<setfill(‘*’)<<a; * * 10 (v) setbase:- This manipulator is used to set base of the input or output value. Setbase(8) Setbase(10) Setbase(16) S.C.E. SAHARSA BY- RONU KUMAR (DEPT OF CSE) 6 Int a; Cin>>a; Cin>>setbase(8)>>a; Cout<<a; Cout<<setbase(10)<<a; Cout<<setbase(16)<<a; Note:- Required header file for setfill, precision and setbase. #include<iomanip.h> ➢ Memory Allocation Operator:- • new • delete in C :- malloc() int *p; p=(type cast)malloc(sizeof(data_type)); p=(int *)malloc(sizeof(data_type)) *p=10; free(p); In C++:- new p=new int; p=new int(10); static/dynamic memory allocation operator.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages23 Page
-
File Size-