Reference no: EM131006542
1. Write a C++ template class named Label that takes a single type parameter named E and supports three methods:
a. A constructor that takes a single argument, which is the value of the label, and stores it in' an instance variable named value.
b. getValue: takes no parameters and returns value.
c. selValue: a void method that takes a single parameter of type E and assigns it to value.
2. The following declarations are given.
class Base
{
friend void Other::baz 0;
public:
virtual void foo (Base obj);
protected:
int data;
private:
int size;
};
class Derived : public Base
public:
virtual void foo (Base obj);
};
Given the following partial main program int main (void)
Base *b = new Derived; Base *a = new Base(*b); // insert statement (x) here
For each of the following pairs of statements (x), state whether they are equivalent to each other (where "equivalence" means that the same methods operate on the same parameter values). For each, give a 1-sentence justification for your answer.
i. b->foo(*a);
ii. b->foo(*b);
Suppose that a is an object of class Base, and b is an object of class Derived, and that both are declared globally. For each statement in the kit column of the table below. indicate whether the statement is allowed in the each of the scopes listed at the heads of the table's other columns. Indicate your answers by writing legal or illegal in the tables cells. Statement : In main() In A:foo0 In B:fosx) In friend function Other::baz
Statement
|
Main()
|
afoo()
|
bfoo()1
|
Other::baz
|
a.size=10;
|
|
|
|
|
b.data=20;
|
|
|
|
|
b.foo(a);
|
|
|
|
|
3. Complex number s are useful in science net and engineering applications. They have the form a *la. where a is a real number and h is an imaginary number. An example is: 1.0+2.51Xse the following class declaration for complex numbers.
*include <iostream>
class complex
public :
Complex(double re = O. double im =():
double real();
double imaginary();
void reall( double re ):
void imaginaryls (double im);
private:
double re;
double im ;
};
istream &operater >> (istream&, complex &c);
a. Explain why operator >> is defined as a nonmember function.
b. Write the code for the complexNumber operator >>. Assume that the format of the number read in is a +bi (including the "+" and the "i".
You may assume the presence of a space between the a and the + and between the b and the i. if you wish.
4 a. List out the difference between the below three lines. (3 Marlin)
- int DoSomething() throw();
- int DoSomething() throw(double);
- int DoSomething() throw(.....);
b. An exception handle: wants to re throw the exception might without processing it. How to handle the situation, explain with example?
5. a. Using new operator, show how to allocate memory for an integer Milner to a pointer in order, to make it store 3 rows and 2 columns of integers
b. With an example explain the concept of return by reference and reference parameter.
6. a. Create two classes, Apple and Orange. In Apple. create a constructor that takes an Orange as an argument Create a function that takes an Apple and call that function with an Orange to show that it works, Now make the Apple constructor explicit to demonstrate that the automatic type conversion is thus prevented. Modify the call to your function so that the conversion is made explicitly and thus succeeds.
b. Explain with an example the use of virtual base class in C++.
7. write a C++. program to keep records and perform statistical analysis for a class of 20 quizzes per semester), midterm score, final score, and total score.
The program will prompt the user to choose the operation of records from a menu as shown below:
MENU:
a. Add student records
b. Delete student records
c. Update student records
d. Son students' records by total score
Note: All students records store in a file stream.
8. A stack is a basic data structure, where insertion and deletion of items takes place at one end called top of the stack. The basic concept can be illustrated by thinking of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. A stack is also called a LIFO (Last in First Out) to demonstrate the way it accesses data. There are basically three operations that can be performed on stacks . They are I) inserting an item into a stack (push). 2) deleting an item from the stack (pop). 3) displaying the contents of the stack(peek), Implement it using templates.
a. throw an exception if stack is underflow( stack is empty, is doesn't have any data and trying to remove)
b. throw an exception if stack is overflow ( data inserted more than the army size)
9. You are going to develop software for the accounts department of a company. It should stow the details of the employees. Details should include the salary and profit share of the employees. The company has a policy. Wm; it will calculate the profit share of each employee by dividing the monthly profit of the company with the total number of employees. Consider the change in the number of employees (new appointments and resignations). Create two Class Company and employee
a. Use static members (function(s) and data member(s)).
b. Use appropriate initialization.
c. Use 'Mine function
d. Use class employee as friend to access the details from class company
Data Members can be: number of employees, profit of the company, salary, profit share, number of employees added or left.
Member functions can be: to get salary, to get profit of the company, to calculate a net salary of an employee, to update number employees, to display employee details.