Oop with c++ only programes pdf download
As stated earlier, OOPs allow for reuse of code and the implementation of methods. However, as an object-oriented programming concept continues to evolve, the ability for code re-use decreases. As such, encapsulation can no longer be achieved as the original object oriented programming concept does not allow for it. This is the reason why encapsulation was introduced.
The final component of object-oriented programming involves the implementation of data members. Encapsulation allows one another to have a safe, secure interaction with the data members. A data member function usually returns a value and performs its duties only once the data member is accessed.
Since data members are never reused, the possibility of data member corruption due to a memory error or system crash is greatly reduced. March 18, April 30, March 18, Hello, I am Muhammad Usman Younas a founder of worldstudypoint.
And to help the Students in their Studies. Skip to content. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables. Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class.
You can define member functions and static data members of a nested class in namespace scope. For example, in the following code fragment, you can access the static members x and y and member functions f and g of the nested class nested by using a qualified type name. Qualified type names allow you to define a typedef to represent a qualified class name. You can then use the typedef with the :: scope resolution operator to refer to a nested class or class member, Types Of inheritance with example?
Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class es Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance. Difference between structure and Union? Union: If we are having the less memory to use in our program, for example 64K, we can use a single memory location for more than one variable this is called union. You can use the unios in the followig locations.
You can share a single memory location for a variable myVar1 and use the same location for myVar2 of different data type when myVar1 is not required any more. You can use it if you want to user, for example, a long variable as two short type variables. When you dont know what type of data is to be passed to a function, and you pass union which contains all the possible data types 1. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.
A structure is combination of different data types. Lets take the exmple of a book, if we cant to declare a book we will be thinking about the name, title, authors and publisher of the book and publishing year. So to declare a book we need to have some complex data type which can deal with more than one data types. Lets declare a Book. We can declare a structure to accommodate the book.
A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc. That is, of operators can be extended to work not just with built-in types but also classes. Is operator overloading really useful in real world implementations? It certainlly can be, making it very easy to write code that feels natural we'll see some examples soon.
In addition, operators tend to have very specific meaning, and most programmers don't expect operators to do a lot of work, so overloading operators can be abused to make code unreadable. But we won't do that.
Complex a 1. We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class as done in the following example by redeclaring the static variable, using the scope resolution operator :: to identify which class it belongs to.
Note on pointer? Pointers are variables that contain memory addresses see Addresses, Pointers, and References. Reference parameters typically used. A string represents a sequence of characters.
Types of operator overloading? There are two types of operator overloading: 1. Unary operator overloading 2. Binary operator overloading 1. Unary operator overloading Unary operators are the ones that operate on one operand, one such operator is the unary minus - operator which is used to change the sign of the operand it acts upon.
This operator works well with basic data types such as int, float, etc.. In this section we will see how to overload this operator so that it can work the same way for user defined data types as it does for basic data types, i. The unary minus operator function does not take any arguments but it changes the sign of the data members of the object that calls this function.
Since this function is a member function of the same class, it can directly access the members of the object that calls it.
Binary operator overloading Binary operators can be overloaded in a similar manner as unary operators. We should note the following features of an operator function for a binary operator: It receives only one class type argument explicitly, in case of a member function. For a friend function, two class types are received as arguments.
It returns a class type. Programs are divided into objects 2. Data structures designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure 4.
Data is hidden and cannot be accessed by external functions 5. Objects may communicate with each other through functions 6. New data and functions can be easily added whenever necessary 7. IT follows bottom-up approach in program design 8. Concentration is on data rather than procedure What are control flow statement? When a program is run, the CPU begins execution at the top of main , executes some number of statements, and then terminates at the end of main.
Straight-line programs have sequential flow — that is, they take the same path execute the same statements every time they are run even if the user input changes. However, often this is not what we desire. This is not possible in a straight-line program. There are quite a few different types of control flow statements.
Private Private is the default access specifier for every declared data item in a class. Private data belongs to the same class in which it is created and can only be used by the other members of the same class. Protected When a data item is declared as protected it is only accessible by the derived class member. Public Public allows to use the declared data item used by anyone from anywhere in the program.
Data items declared in public are open to all and can be accessed by anyone willing to use their values and functions they provide. What is abstract class? In programming languages, an abstract class is a generic class or type of object used as a basis for creating specific objects that conform to its protocol, or the set of operations it supports. Abstract classes are not instantiated directly.
Abstract classes are useful when creating hierarchies of classes that model reality because they make it possible to specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class a derived class is needed. An abstract method will not have any code in the base class; the code will be added in its derived classes. The abstract method in the derived class should be implemented with the same access modifier, number and type of argument, and with the same return type as that of the base class.
Objects of abstract class type cannot be created, because the code to instantiate an object of the abstract class type will result in a compilation error. Merits and Demerits of OOP? OOP stands for object oriented programming. It offers many benefits to both the developers and the users.
Object-orientation contributes to the solution of many problems associated with the development and quality of software products. This leads to saving of development time and higher productivity.
What is function Prototype? Unlike a full definition, the prototype terminates in a semi-colon. Explain pointer arithmathic with example? As you understood pointer is an address which is a numeric value; therefore, you can perform arithmetic operations on a pointer just as you can a numeric value. Technically, a recursive function is a function that makes a call to itself. To prevent infinite recursion, you need an if-else statement of some sort where one branch makes a recursive call, and the other branch does not.
The branch without a recursive call is usually the base case base cases do not make recursive calls to the function. Functions can also be mutually recursive. For example, function f can call function g and function g can call function f. This is still considered recursion because a function can eventually call itself. In this case,f indirectly calls itself. Functions can be tail-recursive. In a tail-recursive function, none of the recursive call do additional work after the recursive call is complete additional work includes printing, etc , except to return the value of the recursive call.
The following is typical of a tail-recursive function return. Hence, additional work. Hence, not tail-recursive. It's common, in tail-recursive functions, to have one of the parameters be pass-by-reference though this is not necessary , which is used to accumulate the answer. Once the base case is reached, you simply return the parameter value, which has the answer.
Another form of abstraction could be metadata used to provide a mapping between two formats that hold structured data. Abstraction is something we do every day e. We abstract the properties of the object, and keep only what we need E.
The implementation issues are deferred to a later time or are provided by somebody else e. As a result, we only have to focus on one aspect at a time. This issue is usually referred to as separation of concerns.
Since we define these new high-level types from an abstract point of view, they may be called abstract types. An abstract type denotes a set of entities characterized by a list of operations that may be applied to them together with a precise specification of each one of these operations. Usually, the list of operations that define a type and their specification are referred to as the type behaviour, type specification or the type contract. An abstract type is also called interface.
The set of entities which share the operations defined for a type are called instances of that type. An example of abstract type may be the type Student.
This is an abstract type whose instances are each one of the specific students Joe, Ann Usually, we will refer to abstract types just as types. This approach is coincident to what we do in everyday life to manage complexity. For instance, cars are very complicated machines. In fact, it would be very difficult, if not impossible, to drive having in mind, at the same time, the functionality expected from the car the what: speed up, change direction, brake, etc.
Encapsulation Objects expose functionality only through methods, properties, and events, and hide the internal details such as state and variables from other objects. This is wrapping up of data and functions into a single unit called class. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it.
This insulation of the data from direct access by the program is called data hiding or information hiding. Therefore encapsulation means putting the data and the function that operates on that data in a single unit information hiding. It is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
Eg:-automatic transmission of an automobile. It encapsulates lots of information about the engine, such acceleration, the pitch of the surface, and the position of the shift lever. Being a user, we have only one method of affecting this complex encapsulation: moving the gear-shift lever. Polymorphism, a Greek term, means the ability to take more than one form. For example, consider the operation of addition.
For two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
This is something similar to a particular word having several different meanings depending upon the context. Using a single function name to perform different type of task is known as function overloading. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface.
This means that a general class of operations may be accessed in the same manner even though specific action associated with each operation may differ. Polymorphism is extensively used in implementing inheritance. There are several different kinds of polymorphism.