Embed
Email

solved

Document Sample

Shared by: naveed ahmed
Categories
Tags
Stats
views:
10
posted:
12/31/2011
language:
pages:
14
Cs304

Subjective type

Midterm 2011

By; MEHRAN ALI SHAH

Mehranali1991@gmail.com

.

Question: What is Abstraction?

Answer: The importance of abstraction is derived from its ability to hide

irrelevantdetails and from the use of names to reference objects.

Abstraction is essential in the construction of programs. It places the

emphasis on what an object is or does rather than how it is represented

or how it works. Thus, it is the primary means of managing complexity in

large programs.

Question: What is a Class Diagram?

Answer: A class diagrams are widely used to describe the types of objects

in a system and their relationships. Class diagrams model class

structure and contents using design elements such as classes, packages

and objects.

Question: What is Method Overriding?

Answer: Method overriding is a language feature that allows a subclass

to override aspecific implementation of a method that is already provided

by one of its super-classes. A subclass can give its own definition of

methods but need to have the same signature as the method in its

super-class. This means that when overriding a method the subclass's

method has to have the same name and parameter list as the super-

class's overridden method.

Question: What is Operator Overloading?

Answer: The operator overloading is a specific case of polymorphisms in

which some or all of operators like +, - or == are treated as polymorphic

(multi) functions and as such have different behaviors depending on the

types of its arguments.

Question: What is Method Overloading?

Answer: The method overloading is the ability to define several methods

(in same class) all with the same name but different on the basis of

i) number of parameters ii) types of parameters.No.7

Question: What is Polymorphisms?

Answer: Polymorphism is a generic term that means 'many shapes'. More

precisely Polymorphism means the ability to request that the same

operations be performed by a wide range of different types of things.

Question: What is Inheritance?

Answer: Ability of a new class to be created, from an existing class by

extending it, is called inheritance.

Question: What is a base class?

Answer: When inheritance is used to create a new class from another,

the new class is called the subclass or derived class, and the class from

which it was derived is called the base class.

Question: What is a concrete class?

Answer: A concrete class is one that can be used to directly create, or

instantiateobjects, unlike an abstract base class which can only be used

as a base class for otherclasses which eventually lead to concrete classes

Question: What are data members?

Answer: Objects are miniature programs, consisting of both code and

data. The codeconsists of a series of member functions. The data items

are called data members.

Question: What is a constructor?

Answer: Objects are complete, miniature programs and, like any good

programs,have well defined initialization and termination phases. They

have special routines (i.e.member functions ) to look after this. The

initialization routine is called the constructor,and C++ ensures that every

object is properly initialized by calling its constructor. The designer of the

object can have more than one constructor, a situation called overloading

and then the compiler will select between them depending on exactly

what arguments are passed to the constructor function. However there

must always be a default constructor,to be used when no information is

supplied.

Question: What is a destructor?

Answer: The termination routine is called the destructor, and C++ will

provide a default if none is supplied. If, during the lifetime of the object,

it uses heap memory then the designer of the object must provide a

destructor function to release such memory to avoid a memory leak.

Question: What is global variable?

Answer: Global variables can be accessed throughout a program.

Another way to put this is to say they have global scope.

Question: What is local variable?

Answer: Local variables can only be accessed within the function, or

morespecifically the compound statement in which they are declared.

Another way to put this is to say they have local scope.

Question: What is a null pointer?

Answer: A null pointer is a pointer that is currently pointing to nothing.

Often pointers are set to zero to make them null pointers or tested

against zero to see if they are null or not.

Question: What is a pointer?

Answer: A pointer is a variable that holds the address of another variable

or object.No.8

Question: What is meant by protected?

Answer: The protected keyword in the class statement means that the

following members of the class are not available to users of the objects of

the class, but can be used by any subclass that inherits from it, and

consequently forms part of its implementation.

Question: What is OOP?

Answer: The object oriented programming is commonly known as OOP.

Most of the languages are developed using OOP concept. Object-oriented

programming (OOP) is a programming concept that uses "objects" to

develop a system. An object hides the implementation details and

exposes only the functionalities and parameters it requires to its client.

Here also an object shares the same concept as that of a bike. While

driving a motor bike, we are unaware of its implementation details such

as how it is developed, internal working of gears etc.? We know only the

functions or actions it can perform.

Question: What are the various elements of OOP?

Answer: Various elements of OOP are:

• Object

• Class Method

• Encapsulation

• Information Hiding

• Inheritance

• Polymorphism

Question: What are the characteristics of Object Oriented

programming language?

Answer: Some key features of the Object Oriented programming are:

Emphasis on data rather than procedure Programs are divided into

entities known as objects Data Structures are designed such that they

characterize objects Functions that operate on

data of an object are tied together in data structures Data is hidden and

cannot be accessed by external functions Objects communicate with

each other through functions New data and functions can be easily

added whenever necessary Follows bottom up design in program design

Question: What are the basic Concepts used in the Object-Oriented

Programming language?

Answer: Object Class Data Abstraction and Encapsulation Polymorphism

Inheritance Message passing Dynamic binding

Question: What Is An Object? (Object-Oriented Technology)

Answer: There are many definitions of an object, such as found in [Booch

91, p77]:"An object has state, behavior, and identity; the structure and

behavior of similar objects are defined in their common class; the terms

instance and object are interchangeable".This is a "classical languages"

definition, as defined in [Coplien 92, p280], where "classes play a central

role in the object model", since they do not in prototyping/delegation

languages. "The term object was first formally applied in the Simula

language, and objects typically existed in Simula programs to simulate

some aspect of reality" [Booch 91, p77]. Other definitions referenced by

Booch include Smith and Tockey: "an object represents an individual,

identifiable item, unit, or entity, either real or abstract, with a

well-defined role in the problem domain." and [Cox 91]: "anything with a

crisply defined boundary" (in context, this is "outside the computer

domain". A more conventional definition appears on pg 54). Booch goes

on to describe these definitions in depth.[Martin 92, p 241] defines: "An

"object" is anything to which a concept applies", and "A concept is an

idea or notion we share that applies to certain objects in our awareness".

[Rumbaugh 91] defines: "We define an object as a concept, abstraction or

thing with crisp boundaries and meaning for the problem at hand."

[Shlaer 88, p 14] defines: "An object is an abstraction of a set of real-

world things such that.

Question: What Is Object Encapsulation (Or Protection)?

Answer: [Booch 91, p. 45] defines: "Encapsulation is the process of

hiding all of the details of an object that do not contribute to its essential

characteristics." [Coad 91,1.1.2] defines: "Encapsulation (Information

Hiding). A principle, used when developing an overall program structure,

that each component of a program should encapsulate or hide

a single design decision... The interface to each module is defined in such

a way as to reveal as little as possible about its inner workings. [Oxford,

1986]" Some languages permit arbitrary access to objects and allow

methods to be defined outside of a class as in conventional

programming. Simula and Object Pascal provide no protection for

objects, meaning instance variables may be accessed wherever visible.

CLOS and Ada

allow methods to be defined outside of a class, providing functions and

procedures. While both CLOS and Ada have packages for encapsulation,

CLOS's are optional while Ada's methodology clearly specifies class-like

encapsulation (Adts). However most objectoriented languages provide a

well defined interface to their objects thru classes. C++ has a very

general encapsulation/protection mechanism with public, private and

protected members. Public members (member data and member

functions) may be accessed from anywhere. A Stack's Push and Pop

methods will be public. Private members are only accessible from within

a class. A Stack's representation, such as a list or array, will

usually be private. Protected members are accessible from within a class

and also from within subclasses (also called derived classes). A Stack's

representation could be declared protected allowing subclass access. C++

also allows a class to specify friends (other (sub)classes and functions),

that can access all members (its representation). Eiffel 3.0 allows

exporting access to specific classes.

Question: What Is A Class?

Answer: A class is a general term denoting classification and also has a

new meaning in object-oriented methods. Within the OO context, a class

is a specification of structure (instance variables), behavior (methods),

and inheritance (parents, or recursive structure and behavior) for

objects. As pointed out above, classes can also specify access

permissions for clients and derived classes, visibility and member lookup

resolution. This

is a feature-based or intensional definition, emphasizing a class as a

descriptor/constructor of objects (as opposed to a collection of objects, as

with the more classical extensional view, which may begin the analysis

process). Original Aristotlean classification defines a "class" as a

generalization of objects: [Booch 91, p93] "a group, set, or kind marked

by common attributes or a common attribute; a group division,

distinction, or rating based on quality, degree of competence, or

condition".

Question: What Is A Meta-Class?

Answer: Meta-Class is a class' class. If a class is an object, then that

object musthave a class (in classical OO anyway). Compilers provide an

easy way to picture Meta-Classes. Classes must be implemented in some

way; perhaps with dictionaries for methods, instances, and parents and

methods to perform all the work of being a class.

This can be declared in a class named "Meta-Class". The Meta-Class can

also provide services to application programs, such as returning a set of

all methods, instances or parents for review (or even modification).

[Booch 91, p 119] provides another example in Smalltalk with timers. In

Smalltalk, the situation is more complexNo.10

Question: What Is Inheritance?

Answer: Inheritance provides a natural classification for kinds of objects

and allows for the commonality of objects to be explicitly taken

advantage of in modeling and constructing object systems. Natural

means we use concepts, classification, and generalization to understand

and deal with the complexities of the real world. See the

example below using computers. Inheritance is a relationship between

classes where one class is the parent base/superclass/ancestor/etc.)

class of another. Inheritance provides programming by extension (as

opposed to programming by reinvention [LaLonde 90]) and can be used

as an is-a-kind-of (or is-a) relationship or for differential programming.

Inheritance can also double for assignment

Question: What Is The Difference Between Object-Based And Object-

Oriented?

Answer: Object-Based Programming usually refers to objects without

inheritance [Cardelli 85] and hence without polymorphism, as in '83 Ada

and Modula-2. These languages support abstract data types (Adts) and

not classes, which provide inheritanceand polymorphism. Ada95 and

Modula-3; however, support both inheritance and polymorphism and are

object-oriented. [Cardelli 85, p481] state "that a language is object-

oriented if and only if it satisfies the following requirements: - It supports

objects that are data abstractions with an interface of named operations

and a hidden local state. - Objects have an associated type. - Types may

inherit attributes from supertypes. object-oriented = data abstractions +

object types + type inheritance These definitions are also found in [Booch

91, Ch2 and Wegner 87]. [Coad 91] provides another model: Object-

Oriented = Classes and Objects + Inheritance + Communication with

messages.

Glossary (Updated Version)

Abstract class : A class that can only be used as a base class for some

other class. A class is abstract if it has at least one pure virtual function.

Access control : A C++ mechanism for prohibiting or granting access to

individual members of a class. See public, private, protected, and

visibility.

Access declaration : A way of controlling access to a specified member

of a base class when it is used in a derived class.

Access specifier : A way of labelling members of a class to specify what

access is permitted i.e public, private, and protected.

Accessor : : A public member subprogram that provides query access to

a private data member.

Agent: : An object that can both initiate behavior in other objects, as well

as be operated upon by other objects.

Allocation: The process of giving memory space to an object. See

dynamic storage,static storage, and deallocation.

ANSI: Acronym for American National Standards Institute, a standards

body currently standardizing C++.

array : : An ordered collection that is indexed.

array constructor: A means of creating a part of an array by a single

statement.

array overflow: : An attempt to access an array element with a subscript

outside the array size bounds.

Array pointer: : A pointer whose target is an array, or an array section.

Array section: : A subobject that is an array and is not a defined type

component.

Assertion: : A programming means to cope with errors and exceptions.

Assignment operator: : The equal symbol, ―=‖, which may be overloaded

by a user.age No.11

Attribute: : A property of a variable that may be specified in a type

declaration statement.

Base class: : A previously defined class whose public members can be

inherited by another class. (Also called a super class.)

Behavior sharing: : A form of polymorphism, when multiple entities

have the same generic interface. This is achieved by inheritance or

operator overloading.

Binary operator: : An operator that takes two operands.

Binary tree: : A tree structure where each node has two child nodes.

Call-by-reference: : A language mechanism that supplies an argument

to procedure by passing the address of the argument rather than its

value. If it is modified, the new value will also take effect outside of the

procedure.

Call-by-value: : A language mechanism that supplies an argument to a

procedure by passing a copy of its data value. If it is modified, the new

value will not take effect outside of the procedure that modifies it.

Class attribute: : An attribute whose value is common to a class of

objects rather than a value peculiar to each instance of the class.

Class descriptor: : An object representing a class, containing a list of its

attributes and methods as well as the values of any class attributes.

Class diagram: : A diagram depicting classes, their internal structure

and operations, and the fixed relationships between them. Class

inheritance: : Defining a new derived class in terms of one or more base

classes.

Class: : An abstraction of an object that specifies the static and

behavioral characteristics of it, including their public and private nature.

A class is an ADT with aconstructor template from which object

instances are created.

Concrete class: : A class having no abstract operations and can be

instantiated.

Constructor: : An operation, by a class member function, that initializes

a newly created instance of a class.

Container class: : A class whose instances are container objects.

Examples include sets, arrays, and stacks. Container object: : An object

that stores a collection of other objects and provides

operations to access or iterate over them.

Data hiding: : The concept that some variables and/or operations in a

module may not be accessible to a user of that module; a key element of

data abstraction.

Information hiding: : The principle that the state and implementation of

an object should be private to that object and only accessible via its

public interface.

Inheritance: : The relationship between classes whereby one class

inherits part or all of the public description of another base class, and

instances inherit all the properties and methods of the classes which

they contain.

Instance: : A individual example of a class invoked via a class

constructor.

Linked list: : A data structure in which each element identifies its

predecessor and/or successor by some form of pointer.

Member data: : Variables declared as components of a defined type and

encapsulated in a class.

Member function: : Subprograms encapsulated as members of a class.

message passing: : The philosophy that objects only interact by sending

messages to each other that request some operations to be performed.

Message: : A request, from another object, for an object to carry out one

of its operations.No.12

Method: : A class member function encapsulated with its class data

members.

object: : A concept, or thing with crisp boundaries and meanings for the

problem at hand; an instance of a class.Object diagram: : A graphical

representation of an object model showing relationships,

attributes, and operations.

Object-oriented (OO): : A software development strategy that organizes

software as a collection of objects that contain both data structure and

behavior.

object-oriented programming (OOP) : Object-oriented programs are

object-based, classbased, support inheritance between classes and base

classes and allow objects to send and receive messages.

Operation: : Manipulation of an object‘s data by its member function

when it receives a request.

Operator overloading: : A special case of polymorphism; attaching more

than one meaning to the same operator symbol. ‗Overloading‘ is also

sometimes used to indicate using the same name for different objects.

Overloading: : Using the same name for multiple functions or operators

in a single scope.

Overriding: : The ability to change the definition of an inherited method

or attribute in a subclass.

Parameterized classes: : A template for creating real classes that may

differ in welldefined ways as specified by parameters at the time of

creation. The parameters are often data types or classes, but may

include other attributes, such as the size of a collection.

(Also called generic classes.)

Pointer: : A single data object which stands such as an array, or defined

type.

Polymorphism: : The ability of an function/operator, with one name, to

refer to arguments, or return types, of different classes at run time.

Private: : That part of an class, methods or attributes, which may not be

accessed by other classes, only by instances of that class.

Protected: : (Referring to an attribute or operation of a class in C++)

accessible by methods of any descendent of the current class.

Public: : That part of an object, methods or attributes, which may be

accessed by other objects, and thus constitutes its interface.

Super class: : A class from which another class inherits.

===========================================================

Static data members.......2.marks

Static Data Member

Definition

―A variable that is part of a class, yet is not part of any object of that

class, is called static data member‖. They are shared by all instances

(objects) of the class. They do not belong to any particular instance of a

class

Class vs. Instance Variable

Suppose we created three objects of student class as shown below,

Student s1, s2, s3; Static Data Member (Syntax) Keyword static is used

to make a data member static

class ClassName{



static DataType VariableName;

};

Defining Static Data Member

Static data member is declared inside the class

But they are defined outside the class

Defining Static Data Member

class ClassName{



static DataType VariableName;

};

DataType ClassName::VariableName;

Initializing Static Data Member

Static data members should be initialized once at file scope

They are initialized at the time of definition

Example

class Student{

private:

static int noOfStudents;

public:



};

int Student::noOfStudents = 0;

/*private static member cannot be accessed outside the class except for

initialization*/

Initializing Static Data Member

If static data members are not explicitly initialized at the time of

definition then

they are initialized to 0

Example

int Student::noOfStudents;Page No.15

is equivalent to

int Student::noOfStudents=0;



Question:13(Marks:3)

Justify your answer with example could it possible ―Array of

Objects‖?

12.6. Array of Objects

• Array of objects can only be created if an object can be created without

supplying an explicit initializer. There must always be a default

constructor if we want to create array of objects

12.4. this Pointer and static member functions

• this pointer is passed implicitly to member functions

• this pointer is not passed to static member functions

• Reason is static member functions cannot access non static data

members

12.5. Global Variable vs. Static Members

• Alternative to static member is to use global variable

• Global variables are accessible to all entities of the program

• User of Global variables is against the principle of information hiding.

Give the names of any two types of template?

In C++ generic programming is done using templates.

Templates are of two kinds,

a. Function Templates (in case we want to write general function like

printArray)

b. Class Templates (in case we want to write general class like Array

class)

Compiler generates different type-specific copies from a single template

This concept is similar to concept of making prototype in the form of

class for all objectsof same kind.



a. What is wrong in the definition of member function doSomething(

).

Answer:

Static keyword because using static key word we are declaring a static

member function doSomething(). Which can not use non static data

members.

b. What will be the effect of writing the friend function operator<<(…) in

private part of the above class? 02

Answer:

There will be no effect on friend function if we write it in private part.

Friend function is a friend function and can use any private or public

data member of the class. Where ever we declare it in class body.

Question No: 6 ( Marks: 5 )

Write any two advantage(s) of declaring a member function as

const?

Constant member functions

• This will enable constant objects to access more member functions

• Allow to access const data member of the class.

• It does not allow function to change the value of data member.

Question No: 19 ( Marks: 2 )

Explain two benefits of setter functions.

Objects can make their data invisible (in accordance with the principle of

data hiding).Setters and getters functions are provided by class to access

the its members it also minimizes the changes to move the objects in

inconsistent state as we can write checks in our setter functions for

example

• We can check that whether the user has entered correct age value and

has not entered negative value for age.

• Object remains in consistent state.

It is against the principle of OOP to access the data members directly

using object of class as we have done above. This code is given for

example only we should write assessor functions (setters and getters)

wherever we want to access the members of the class.

Question

Write the syntax of declaring a pure virtual function in a class?

Ans:

Pure Virtual Function is a Virtual function with no body .Declaration of

Pure Virtual Function. Since pure virtual function has no body, the

programmer must add the notation =0 for declaration of the pure virtual

function in the base class.



Question No: 28 ( Marks: 2 )

State any two reasons why the virtual methods can not be static?

1-virtual method can not be static as it is dynamic

2-as virtual method is dynamic so it works automatically that is also

another reason

That virtual method can not be static.

Question No: 30 ( Marks: 2 )

Explain two benefits of setter functions.

1- It minimize the changes to move the objects in inconsistent states

2- You can write checks in your setter functions to check the validity of

data entered by the user, for example age functions to check to calculate

the age from date entered.

Question No: 32 ( Marks: 3 )

What do you mean by Stack unwinding?

When we want to check what happens actually to the local variables in

the try block when then an exception is thrown this concept is called

stack unwinding.

Question No: 34 ( Marks: 5 )

What is random_iterator? What is relation between random_iterator

and Vector?

Random iterator: it provided both increment and decrement and also

provide constant time methods for moving forward and backword in

arbitrary sized steps. Ramdom iterator provide asentially all of the

operations of ordinary c pointer arithmetic.Vector class provide an stl

style random access iterator for use with generic algorithm .since neither

the vactor nor the matrix classes are container classes in actuall. The

iterator class is really an iterator of data object that is viewed by vector or

matrix.

Question No: 35 ( Marks: 3 )

Describe three properties necessary for a container to implement

Generic Algorithms.

If you declare a container as holding pointers, you are responsible for

managing the memory for the objects pointed to. The container classes will

not automatically free memory for these objects when an item is erased

from the container.

Container classes are expected to implement methods to do the following

create a new empty container (constructor),

report the number of objects it stores (size),

delete all the objects in the container (clear),

insert new objects into the container,

remove objects from it,

provide access to the stored objects.

Question No: 20 ( Marks: 3 )

What do you mean by the term Abstraction? Why we use it.

Abstraction is way to cope with complexity and it is used to simplify things.

Principle of abstraction:

Capture only those details about an object that are relevant to current perspective

Abstraction

Example:

Suppose we want implement abstraction for the following statement, "Ali is a PhD

student and teaches BS Students"

Here object Ali has two perspectives one is his student perspective and second is his

teacher perspective



Abstraction Advantages,

Abstraction has following major advantages,



1. It helps us understanding and solving a problem using object oriented approach as it

hides extra irrelevant details of objects.

2. Focusing on single perspective of an object provides us freedom to change

implementation for other aspects of for an object later.

Similar to Encapsulation Abstraction is also used for achieving information hiding as we

show only relevant details to related objects, and hide other details.







Question No: 21 ( Marks: 5 )



Derived class can inherit base class features? Explain it with example.



Inheritance is a process by which an object inherits parent

Object quality. inheritance gives reusability, The derived classes have all the features of

the base class and the programmer can choose to add new features specific to the newly

created derived class. The relationship between a parent and child class under private

inheritance¤ is not "is a", but "is implemented as a"



Example: father and child relation. father properties power

Get the child.



Question No: 21

Difference between ++ post and prefix operators?

In prefix, first the value is incremented and then the value is assigned.

In postfix, first the value is assigned and then incremented in post fix first the condition

check then increment and in prefix first the increment than check the condition. It is

important to note that a postfix increment or decrement expression evaluates to the value

of the expression prior to application of the respective operator. The increment or

decrement operation occurs after the operand is evaluated. This issue arises only when

the postfix increment or decrement operation occurs in the context of a larger expression.



The Need for Friend Function:

As discussed in the earlier sections on access specifiers, when a data is declared as private inside a

class, then it is not accessible from outside the class. A function that is not a member or an

external class will not be able to access the private data. A programmer may have a situation

where he or she would need to access private data from non-member functions and external

classes. For handling such cases, the concept of Friend functions is a useful tool.



What is a Friend Function?

A friend function is used for accessing the non-public members of a class. A class can allow non-

member functions and other classes to access its own private data, by making them friends. Thus,

a friend function is an ordinary function or a member of another class.



How to define and use Friend Function in C++:

The friend function is written as any other normal function, except the function declaration of

these functions is preceded with the keyword friend. The friend function must have the class to

which it is declared as friend passed to it in argument.


Related docs
Other docs by naveed ahmed
solved
Views: 10  |  Downloads: 0
Lecture 25
Views: 3  |  Downloads: 0
OOPfreshppr2010
Views: 3  |  Downloads: 0
Lecture 24
Views: 3  |  Downloads: 0
CS304MegasolvedFileMidTermPreparation
Views: 1  |  Downloads: 0
Lecture 45
Views: 16  |  Downloads: 0
CS304_MID_SPRING2007_S1
Views: 1  |  Downloads: 0
Lecture 35
Views: 3  |  Downloads: 0
Helping_Material_of_CS304
Views: 2  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!