Object Orinted Programming Solution PDF

Title Object Orinted Programming Solution
Course Object Oriented Programming
Institution Gujarat Technological University
Pages 61
File Size 1.2 MB
File Type PDF
Total Downloads 429
Total Views 730

Summary

Download Object Orinted Programming Solution PDF


Description

UNIT-1 Difference between C and C++

3

C (Procedure Oriented Programming) Program is divided into small parts called functions. Importance is not given to data but to functions as well as sequence of actions to be done. Follows Top Down approach.

4

It does not have any access specifier.

5

Data can move freely from function to function in the system. To add new data and function in POP is not so easy. Most function uses Global data for sharing that can be accessed freely from function to function in the system. It does not have any proper way for hiding Data so it is less secure.

1 2

6

7 8

9

10

Overloading is not possible. Example of Procedure Oriented Programmings are: C, VB, FORTRAN, Pascal.

C++(Object Oriented Programming) Program is divided into parts called objects. Importance is given to the data rather than procedures or functions because it works as a real world. OOP follows Bottom Up approach. OOP has access specifiers named Public, Private, Protected, etc. Objects can move and communicate with each other through member functions. OOP provides an easy way to add new data and function. In OOP, data cannot move easily from function to function, it can be kept public or private so we can control the access of data. OOP provides Data Hiding so provides more Security. In OOP, overloading is possible in the form of Function Overloading and Operator Overloading. Examples of Object Oriented Programming are: C++, JAVA, VB.NET, C#.NET.

Evolution of C++ • • • • •

C++ began as an expanded version of C. The C++ extensions were first invented by Bjarne Stroustrup in 1979 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++. C was one of the most liked and widely used professional programming languages it becomes more complexity. Because C, once a program exceeds from 25,000 to 100,000 lines of code, it becomes so complex that it is difficult to grasp as a totality, The purpose of C++ is to allow this.

• • • •

The essence of C++ is to allow the programmer to manage larger, more complex programs. Most additions made by Stroustrup to C support object-oriented programming, sometimes referred to as OOP. Stroustrup states that some of C++'s object-oriented features were inspired by another object-oriented language called Simula67.

The Object Oriented Technology (OR) Key Concepts of Object Oriented Programming • It based on the concept of "objects", which can contain data and code • Data in the form of fields and code, in the form of procedures . • The Object Oriented Technology have some key concepts such as 1.Classes 2.Encapsulation 3.Abstraction 4.Inheritance 5.Polymorphism

Class • A class is like a blueprint for an object. • It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. Object • An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. • Syntax ClassName ObjectName; Encapsulation • In Object-Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.

Abstraction • Abstraction means displaying only essential information and hiding the implementation details or background details. Polymorphism • The word polymorphism means having many forms. • In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Example of polymorphism

Inheritance • The capability of a class to derive properties and characteristics from another class is called Inheritance. • Sub Class: The class that inherits properties from another class is called Sub class or Derived Class. • Super Class:The class whose properties are inherited by sub class is called Base Class or Super class. • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.

Dynamic binding • In dynamic binding, the code to be executed in response to function call is decided at runtime • this is also called dynamic binding or late binding or run-time binding. • Dynamic binding is achieved using virtual functions. • Base class pointer points to derived class object. And a function is declared virtual in base class, then the matching function is identified at run-time using virtual table entry. Message passing • Objects communicate with one another by sending and receiving information to each

• •

other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function and the information to be sent.

Conventional Programming • Conventional programming writing a program in a traditional procedural language such as assembly language or high level compiler language ( C , Pascal , COBOL , FORTRAN ,etc ). • The disadvantage is that it doesn’t help to manage program complexity at scale. • As your application grows substantially in size, it becomes more and more difficult to maintain the structure of your program in an understandable fashion, in a way that makes it easier to extend functionality. Advantage of OOP • Data Re-usability • Data Redundancy • Easy Maintenance • Data hiding • Security Object Oriented Language. • The first object-oriented programming language, the most popular OOP languages are: • Java • JavaScript • Python • C++ • Visual Basic .NET • Ruby • Scalable(scala) • PHP

UNIT-2 Classes in C++ •

A class is like a blueprint for an object.



It is a user-defined data type, which holds its own data members(variables) and member functions(methods), which can be accessed and used by creating an instance of that class(object).



The general form of a simple class declaration is class class-name { private data and functions public: public data and functions } object name list;

Of course, the object name list may be empty. •

A class may contain private as well as public parts.



This is one way that encapsulation is achieved. The Example as fallows #define SIZE 100 class stack { int stck[SIZE]; int tos; public: void init(); void push(int i); int pop(); };



To make parts of a class public .public can be accessed by all other functions in the program. public keyword is followed by a colon.



The functions init( ), push( ), and pop( ) are called member functions because they are part of the class stack. The variables stck and tos are called member variables (or data members).

. Only member functions have access to the private members of their class. Thus, only init( ), push( ),and pop( ) may access stck and tos. Declaring Objects •

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.



Syntax ClassName ObjectName;



Once you have defined a class, you can create an object of that type by using the class name.



In essence, the class name becomes a new data typespecifier.



For example,this creates an object called mystack of type stack: stack mystack;



When you declare an object of a class, you are creating an instance of that class. In this case, mystack is an instance of stack.



When you refer to a member of a class from a piece of code that is not part of the class, you must always do so in conjunction with an object of that class. To do so, use the object's name, followed by the dot operator, followed by the name of the member.



This rule applies whether you are accessing a data member or a function member.



For example, this calls init( ) for object stack1. stack stack1; stack stack1,stack2,stack3; stack1.init(); Stack2.init(); Stack3.init();



One last point: Recall that the private members of an object are accessible only by functions that are members of that object. For example, a statement like stack1.tos = 0; // Error, tos is private. Accessing Data Members



The public data members are also accessed in the same way given however the private data members are not allowed to be accessed directly by the object.

// C++ program to demonstrate // accessing of data members #include using namespace std; class Geeks { // Access specifier public: // Data Members string geekname; // Member Functions() void printname() { cout...


Similar Free PDFs