Write a class with one virtual function and one non-virtual


Please use codeblocks

List the program and the output on the command line

Eg

#include

#include

using namespace std;

int main()

{

floatjohno = 0.5;

size_t size = 24;

vector array(size);

for(inti=0; i

    {

johno = johno + 1;

array[i] = johno;

cout<

cout<< " ";

}

}

564_Untitled.png

Q1

Write a class with one virtual function and one non-virtual function. Inherit a new class, make an object of this class, and upcast to a pointer of the base-class type. Use the clock( ) function found in to measure the difference between a virtual call and non-virtual call. You'll need to make multiple calls to each function inside your timing loop in order to see the difference.

Q2

Create two classes called Traveler and Pager without default constructors, but with constructors that take an argument of type string, which they simply copy to an internal string variable. For each class, write the correct copy-constructor and assignment operator. Now inherit a class BusinessTraveler from Traveler and give it a member object of type Pager. Write the correct default constructor, a constructor that takes a string argument, a copy-constructor, and an assignment operator.

Q3

Create two identical functions, f1( ) and f2( ). Inline f1( ) and leave f2( ) as an out-of-line function. Use the Standard C Library function clock( ) that is found in to mark the starting point and ending points and compare the two functions to see which one is faster. You may need to make repeated calls to the functions inside your timing loop in order to get useful numbers.

Q4

Create a Text class that contains a string object to hold the text of a file. Give it two constructors: a default constructor and a constructor that takes a string argument that is the name of the file to open. When the second constructor is used, open the file and read the contents into the string member object. Add a member function contents( ) to return the string so (for example) it can be printed. In main( ), open a file using Text and print the contents.

Q5

Create a Hen class. Inside this, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display( )member function. In main( ), create an instance of each class and call the display( )function for each one.

Q6

Write a function that takes a char* argument. Using new, dynamically allocate an array of char that is the size of the char array that's passed to the function. Using array indexing, copy the characters from the argument to the dynamically allocated array (don't forget the null terminator) and return the pointer to the copy. In your main( ), test the function by passing a static quoted character array, then take the result of that and pass it back into the function. Print both strings and both pointers so you can see they are different storage. Using delete, clean up all the dynamic storage.

Q7

Create a vector and put the following 25 floating-point numbers into it using a for loop:

0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 11.5

12.5 13.5 14.5 15.5 16.5 17.5 18.5 19.5 20.5 21.5

22.5 23.5 24.5

 

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Write a class with one virtual function and one non-virtual
Reference No:- TGS01604285

Now Priced at $30 (50% Discount)

Recommended (93%)

Rated (4.5/5)