Sunteți pe pagina 1din 12

Object Oriented Programming in C++

Object Oriented Programming in C++ STRUCTURE OF MARKING SCHEME

Q1:

Difference between C & C++ : ------------------------------------------------10 Partition must be done 5 prominent differences ------------------------------------------- 06 Explanation------------------------------------------------------------04

Q2 :

Characteristics of object oriented programming-------------------------10 6 subtitles--------------------------------------------------------------02 Explanation-----------------------------------------------------------08

Q3:

a. Structures for secure data----------------------------------------01 b. operator overloaded------------------------------------------------01 c. function can be 1-value in C++----------------------------------01 d. C++ supports composition---------------------------------------01 e. functions can have default arguments----------------------01

Q4 :

Function Polymorphism------------------------------------------------------10 Relate function overloading to polymorphism---------------02 Explain Function prototype--------------------------------------02 Explain function polymorphism---------------------------------04 Example---------------------------------------------------------------02

Q5 :

stages of C++ project and program development------------------------10 Stages explained clearly---------------------------------------------10

Object Oriented Programming in C++

Q6 a.

References and pointer---------------------------------------------------------05 Definition-------------------------------------------------------------01 Syntax------------------------------------------------------------------01 Explanation-----------------------------------------------------------01 Pointer explanation-------------------------------------------------01 2 differences-----------------------------------------------------------01

Q6.b. Cascading of insertion and extraction operation--------------------------05 Definition of << -------------------------------------------------------------01 Example---------------------------------------------------------------------11/2 Definition of >>--------------------------------------------------------------01 Example--------------------------------------------------------------------11/2 Q7. Short notes----------------------------------------------------------------------------10 Inline functions--------------------------------------------------------------04 Language feature and language system feature----------------------04 User defined datatypes in C++------------------------------------------02

Object Oriented Programming in C++

Q1 Difference between C and C++

Object Oriented Programming in C++

Q1. C++
1) C++ is a superset of C

C C is a superset of B.

C was invented by K&R in 1970s.


2) C++ was designed by Bjarne Strarpstrup in 1983.

C follows the procedural programming paradigm Data is not secured.

3) C++ is a multiparadigm language(procedural as well as object oriented. 4) Data is secured(hidden).

C is a low level language.

5) C++ is middle level language.

C uses top-down approach.

6) C++ uses bottom-up approach

C is function driven

7) C++ is object driven

C does not support function overloading.

8) C++ supports function overoading

absent

9) Namespace feature is present

printf scanf

10) Cout<< and cin>> are the I/O functions

C does not.

Object Oriented Programming in C++ 11) C++ allows reference variables. Reference variables allow 2 variables names to point to the same memory

Object Oriented Programming in C++

Q 2. Characteristics of object-oriented programming : list and explain:


Objects: Objects are the basic run time entities in an object oriented system. They may represent a person, place, bank account, table of data or any item that the program has to handle objects takeup space in memory. Classes : These are the building blocks of OOPL. It is the collection of data and functions operates on that data. Classes are the encapsulation of data and function. Data Abstraction: Abstraction refers to the act of representing essential features without including the background details or explanations. Data Encapsulation: The wrapping up of data and functions into a single unit(called classs) is known as encapsulation. Data encapsulation means data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions providethe interface between the objects data and the program. Inheritance: This is the process by which a class can be derived from a base class with all features of base class and some of its own. This increases code reusability. Polymorphism: It is characteristic using which we can assign a single name to multiple functions.
1) Function overloading 2) Operator overloading 3) Virtual functions 4) Dynamic Binding.

Q3 (a) Benefits of function prototype:

Object Oriented Programming in C++

Function prototyping is necessary in C++. A prototype describes the functions interface to the compiler. It tells the compiler the return type of the function as well as the number, type and sequence of its formal arguments. Syntax:Return-type function_name(argument_list); By making prototyping necessary, the compiler ensures the following:

The return value of a function is handled correctly. Correct member and type of arguments are passed to a function.

C++ compiler will report an error message if the function call when no prototype has been provided to resolve the function call. Thus function-prototyping guarantees protection from errors arising out of incorrect function calls.

(b) (a) Structures enable a programmer to secure data :Because of the encapsulation property of C++ security of data is enabled. This prevents any other functions to alter the contents of the structure. (b) left shift & right shift are overloaded:Actually it is used to manipulate the individual bits of data. But it is used as input and output the streams. Thus it is overloaded. (c) I-value in C++ :A function can be a i-value in C++, we are free to write a function towards the left side of an assignment operator. (d) Composition :The process of building complex objects from simpler ones is called composition. By using classes as member variables in other classes as member variables in other classes C++ allows us to do object composition in very simple way. (e) Default arguments:-

Object Oriented Programming in C++

If no value is passed for an argument when the function is called, the default-value specified for it is passed. If parameters are passed in the normal fashion for such an argument, te default value is ignored.

Q4. Function Polymorphism with examples :Function polymorphism can be referred to as the function overloading feature of C++. C++ allows two or more functions to have the same name. for this, however, they must have different signatures. Signatures of a function means the number, type and sequence od formal arguments of the function. Depending upon the type of parameters that are passed to the function call, the compiler decides which of the available definitions will be invoked. For this, function prototypes should be provided to the compiler for matching the function calls, links the function call with the correct function definition. (eg) #include<iostream.h> int add(int, int); int add(int,int,int) void main() { int x,y; x= add(10,20); y=add(30,40,50); cout <<x<<endl<<y<<endl; } int add(int a, int d, int c) { Return(a+b+c); }

Object Oriented Programming in C++

Output: 30 120 Just like ordinary functions, the definitions of overloaded functions are also put in libraries. Moreover, the function prototypes are placed in header files. The two function prototype at the beginning of the program tell the compiler the 2 different ways in which the add() function can be called. When the compiler encounters the two distinct calls to the add() function, it already has the prototypes to satisfy them both. Thus the compilation phase is completed successfully. During linking, the linker finds the 2 necessary definitions of the add() function, and hence links successfully to create the executed file. x= add(10,20); it decides that the function that takes 2 integers as formal arguments is to be executed. Accordingly, the linker then searches for the definition of the add() function where there are two integers as formal arguments. Similarly the second call to the add() function. y= add(30,40,50); is also handled by the compiler and the linker. Thus the function overloading is possible because of the necessity to prototype functions. Function overloading is also known as function polymorphism because , just like polymorphism in the real world where an entity exists in more than one form, the same function name carries different meaning.

Q5 various stages of C++ project and program development :


C++ requires that private implementation details of a class be declared in the same declaration in the same declaration as the public interface details. If a change is made to a private definition, then all source code files that depend on the class definition are recompiled.
1)

Certain changes to the public interface definitions unnecessarily cause recompilation. For eg: adding a public method should not cause dependent objects to be builtthe interface has not changed in an incompatible manner.
2) 3)

C++ must explicitly include definitions for any classes that are required to

compile.
9

Object Oriented Programming in C++ 4)

Source files should include only those definitions that are necessary in

order to compile. Many C++ systems do not support pre-compiled headers. Such systems spend a lot of time analzing header definitions code.
5)

Q6 (a) Explain references and the deference between the references and pointers:
A reference variable is nothing but a reference for an existing variable. It shares the memory location with an existing variable. Syntax: <data-type>&<ref-var-name>=<existing-var-name>; For eg : int & iRef=x; iRef is a reference to x. this means that although iRef and x have separate entries in the OS, their addresses are actually the same. Thus a change in the value of x will naturally reflect in iRef and viceversa. Reference variables must be initialized at the time of declaration. Pointers: Pointers is a special kind of variable in C and C++ that holds the address of another variable. Pointers can refer to null object. But reference must always refer to some object. Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference always refers to the object with which it is initialized.

6(b) justify C++ allowing cascading insertion and extraction operations an streams :
The operator (<<) applied to an input stream is known as insertion operator. It performs an output operation on a stream generally involving some sort of formatting of the data.

10

Object Oriented Programming in C++

It is possible to cascade the insertion operation in C++. That is instead of using cout number of times by cascading it can be used only once. (eg) cout<< something<< is << x; The operator >> applied to an input stream is known as extraction operator. It performs input operation on a stream generally involving some sort of interpretation of the data. It is possible to call a succession of extraction operation as Cin>>variable1>> var2>>var3;

Q7 Explain the following :


(a) Inline Functions :-

An Inline functions is a function whose compiled code is inline with the rest of the program. That is, the compiler replaces the function call with the corresponding function code. With the inline code, the program does not have to jump to another location to execute the code and then jump back. Inline functions, thus, run a little faster than regular functions. For specifying an inline function you must:

Prefix the definition of the function with the inline keyword and Define the function before all functions that call it, that is define it in the header file itself.

(b) Language feature :

Language feature are those attributes which are defined within the construct of the language and not in any library. The values of such features are unchangeable and they form a part of the language itself. All fundamental datatypes, for (eg)., int, char, float, double are language features. Language system features, on the other hand, are those features which have been defined in the libraries of the language. These can be explicitly changed and their values can be modified by the programmer. All predefined functions are language system features (eg)., printf, cin, scanf etc.,

11

Object Oriented Programming in C++

(c) User defined datatypes in C++:

To redefine the name of an existing data type, you use the typedef keyword. The syntax is typedef knownDatatype NewName; The typedef keyword is required to let the compiler know that you are redefining an existing data type. Datatype could be an int, an unsigned int, a char, a double etc., while a datatype can be made of one word or more than one word the new name you are creating for the datatype must be in one word. Eg:typedef int numberOfStudents; In this case, NumberOfStudents is just a new name for an int. it can be used as a newdata type exactly as if you were using an int.

12

S-ar putea să vă placă și