Assinment cs301
Assinment cs301
Assinment cs301
Question: can binary operators overloaded, how can they overload give an
example for any one operator.
Give one-line definition of "Object Orientation".
A type of programming in which programmers define not only the data type
of a data structure, but also the types of operations (functions) that can be
applied to the data structure. In addition, programmers can create
relationships between one object and another. For example, objects can
inherit characteristics from other objects.
a. Public members of base class are _____ public _____ members of derived
class
Protected members of base class are __ protected or private
________members of derived class.
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.
Consider a class Man derived class of Monkey an object ofMan inherits some
of monkeys qualities and overrides some Qualities vukiduniya.com like
walking straight with two legs and have Additional functions like speech etc..
The simple example in C++ is having a class that inherits a data member
from its parentclass.
class A
{ public: integer d;
};
class B : public A
{ public:
};
The class B in inherits the data member d from class A. When one class
inherits from another, it acquires all of its methods and data. We can then
instantiate an object of class B and call into that data member.
void func() { B b; b.d = 10; };
Part A.
Suppose we have a furniture store with the following types of furniture,
Chairs, Tables, Computer Tables, Dining Tables and Beds.
1. You have to model this store using inheritance by describing base class,
derived classes.
2. You also have to describe the IS–A relationship between these classes.
Part B.
What is IS-A relationship, show how it is implemented using c++ code (you
do not have to give the detailed code simply show in a single line or two how
it will be implemented).
In knowledge representation and object-oriented programming and design,
is-a is a relationship where one class D is a subclass of another class B (and
so B is a superclassof D). In object-oriented programming the is-a
relationship arises in the context of inheritance concept. One can say that
"apple" may inherit all the properties common to all fruits, such as being a
fleshy container for the seed of a plant.
The is-a relationship is contrasted with the has-a relationship, which
constitutes the hierarchy. It may also be contrasted with the instance-of
relation: see type-token distinction.
When designing a model (e.g., a computer program) of the real-world
relationship between an object and its subordinate, a common error is
confusing the relations has-a and is-a.
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.
class classname //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function
in C++
};
Ans”
The act of creating a new definition of a function, class, or member of a class
from a template declaration and one or more template arguments is called
template instantiation. The definition created from a template instantiation is
called a specialization. A primary template is the template that is being
specialized.
create function objects to do the case-insensitive compares, and
then reuse them when also wanting to do case-insensitive sorting
or searching.
Question No: 36 ( Marks: 3 )
Can we use compiler generated default assignment operator in case our
class is using dynamic memory? Justify your answer.
Ans:
the compiler does not make a separate copy of the object. Even if the types
are not the same, the compiler is usually able to do a better job with
initialization lists than with assignments.
Consider the following constructor that initializes member object x_ using an
initialization list: square::square() : x_(whatever) { }. The most common
benefit of doing this is improved performance. For example, if the expression
whatever is the same type as member variable x_, the result of the
whatever expression is constructed directly inside x_ — the compiler does
not make a separate copy of the object. Even if the types are not the same,
the compiler is usually able to do a better job with initialization lists than
with assignments.
As if that wasn't bad enough, there's another source of inefficiency when
using assignment in a constructor: the member object will get fully
constructed by its default constructor, and this might, for example, allocate
some default amount of memory or open some default file. All this work
could be for naught if the whatever expression and/or assignment operator
causes the object to close that file and/or release that memory (e.g., if the
default constructor didn't allocate a large enough pool of memory or if it
opened the wrong file).
Ans:
The function will throw DivideByZero as an exception that can then be
caught by an exception-handling catch vukiduniya.com statement that
catches exceptions of type int. The necessary construction for catching
exceptions is a try catch system. If you wish to have your program check for
exceptions, you must enclose the code that may have exceptions thrown in a
try block.
The catch statement catches exceptions that are of the proper type. You
can, for example, throw objects of a class to differentiate between several
different exceptions. As well, once a catch statement is executed, the
program continues to run from the end of the catch.
the errors can be handled outside of the regular code. This means that it is
easier to structure the program code, and it makes dealing with errors more
centralized. Finally, because the exception is passed back up the stack of
calling functions, you can handle errors at any place you choose.
int main(){
Derived * derived = new Derived();
return 0;
}
Fill the table below to tell which member functions of Base and Derived
classes we can access using the Derived pointer in the code indicated in
bold.
Ans:
Function Name
Availability (Yes / No)?
base2()
no
base3()
yes
derived1()
No
derived2()
No
derived3()
Yes
int main()
{
try
{
cout ”Trying.\n”;
sample_function(98.6);
cout “Trying after call.\n”;
}
catch(int)
{
cout “Catching.\n”;
}
cout “End program.\n”;
return 0;
}
void sample_function(double test) throw (int)
{
cout “Starting sample_function.\n”;
if(test < 100)
throw 42;
}
Ans:
Starting sample_function
Trying
Trying after call
Catching
End program
the C++ Standard Template Library contains the function template max(x,
y) which creates functions that return either x or y, whichever is larger.
max() could be defined like this:
template
T max(T x, T y)
{
return x < y ? y : x;
}
#include
#include
using namespace std;
class Shape{
public:
void Draw(){cout"shape"endl;}
};
class Line : public Shape{
public:
void Draw(){cout"Line"endl;}
};
class Circle : public Shape{
public:
void Draw(){cout"Circle"endl;}
};
int main(int argc, char *argv[])
{
Shape * ptr1 = new Shape();
Shape * ptr2 = new Line();
Shape * ptr3 = new Circle();
ptr1->Draw();
ptr2->Draw();
ptr3->Draw();
system("PAUSE");
return 0;
}
Shape
Line
Circle
class B : public A
{
public:
void method() { cout"B's method\n"; }
};
Ans:
t.test(a);
t.test(b);
}
}
Exceptions in Destructors:
An object is presumably created to do something. Some of the changes
made by an object should persist after an object dies (is destructed) and
some changes should not. Take an object implementing a SQL query. If a
database field is updated via the SQL object then that change should persist
after the SQL objects dies. To do its work the SQL object probably created a
database connection and allocated a bunch of memory. When the SQL object
dies we want to close the database connection and deallocate the memory,
otherwise if a lot of SQL objects are created we will run out of database
connections and/or memory.
The logic might look like:
Sql::~Sql()
{
delete connection;
delete buffer;
}
Let's say an exception is thrown while deleting the database connection. Will
the buffer be deleted? No. Exceptions are basically non-local gotos with
stack cleanup. The code for deleting the buffer will never be executed
creating a gaping resource leak.
Special care must be taken to catch exceptions which may occur during
object destruction. Special care must also be taken to fully destruct an
object when it throws an exception.
Abstraction Advantages,
Abstraction has following major advantages,
class B : public A
{ public:
};
The class B in inherits the data member d from class A. When one class
inherits from another, it acquires all of its methods and data. We can then
instantiate an object of class B and call into that data member.
void func() { B b; b.d = 10; };
You can Also Download Midterm Solved Past Papers referenceby Moaaz and
Waqar Sidhu, Final term Solved Past Papers reference by Moaaz and
WaqarSidhu, GDB Solutions, Assignment Solutions, and Solved Quizzes
files, PPTSlides, Handouts, Final term Short Notes, Mid Term Short Notes
and NTS Guidelines.
08Apr2018
Tags: Short Notes for Midterm
Share to:
Share
Next
CS401 Short Notes for Mid Term
Previous
CS302 Short Notes for Mid Term
RELATED POSTS