Mock exam help please...

Hey guys I'm in my first year studying c++ and I have the questions for a mock exam in which I didn't too too well in :( , I was wondering if any of you seasoned c++ professionals could help me with some of the questions that I had trouble with.
Many thanks in advance.

The question is:

a) Write a function long int mypow(int m , int n) that calculates mn

b) How does the parameter set of mypow(..) change when it is called by reference?

c) Write an example in which this function is invoked.

d) The following class member variable is wrongly initialised, please give two ways for correctly doing it.

class Myclass
{
private:
int value;
string name = "hello";
public:
...
};



I hope somebody can help me :)

a)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>

using namespace std;
//this lets the compiler know that the func exists
long int mypow(int m, int n);

int main ()
{


	cout<<mypow(8,6);
	getchar();

}

long int mypow(int m, int n)
{
	long int ReturnValue=(long int)m;


	for (int Itter=0;Itter<n-1;Itter++ )
	{
		ReturnValue*=(long int)m;
	}


	return ReturnValue;
}


b)very bad question wording, but the numbers you passed in then are the m or n's initializers.
c) this is cout<<mypow(8,6);
d) use a constructor or
1
2
Myclass a;
a.ChangeName("hello");

this method is in public and changes the value
1
2
3
4
5
6
7
8
9
10
11
12
13

class Myclass
{
private:
int value;
string name;
public:
void ChangeName(string peram)
{
name=peram;
}
};

Or something like that...
Last edited on
Thanks alastairl for the rapid response, the bad question wording maybe that the course is in Germany.
I am now going to put your answers into my compiler to see if I can understand it better.

I wasn't going to post the others but seen as this got answered so fast why not :)

Next question:

The following set of classes raises three compilation errors. Where do these errors occur and what should be done to avoid them?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyClass1

{
    private:
              int x; int y;
    public:
            virtual void mymethod() = 0;
               //some more member functions to follow
};

class MyClass2 : public MyClass1

{
     private:
               int a; int b;
      public: 
               void setparam(int a, int b)
            {
                 x = a; y = b;
};

----------------------------------------------------------------

Also how long did it take you to fully understand this and go from beginner to well not a beginner.

Many thanks in advance!
Year and a half.

Ok, I can't evaluate this code and tell you whats wrong If I dont know the purpose of the code. I will give it a go though.

virtual void is designed to be replaced by another method in the inherited class.
Not sure what you wanted to achieve by this?
I have shown inheritance of object members x and y in this example on an asumption that this it what you wanted, please be clear next time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

class MyClass1
{
    public:
		int x; 
		int y;
};

class MyClass2 : public MyClass1
{
	private:
                 //Purpose of a and b is nothing!!!
		int a; 
		int b;
	public: 
		void setparam(int a, int b)
			{
				x = a; 
				y = b;
			}
};


int main()
{
	MyClass2 Object1;
	Object1.setparam (5,6);
	cout << Object1.x<<","<< Object1.y;
	getchar();

	
}
Last edited on
Hey, thanks again, how I wrote the question and code down is exactly how its shown on the paper, a year and a half you say...wow I have quite a way to go yet then
correct me if im wrong but void setparam is missing a closing curley brace
1
2
3
class Myclass{
private:
  string name = "hello";
¿Isn't this allowed in the new standard?
1
2
Myclass a;
a.name="hello";
This will not work as 'name' is private.

The following set of classes raises three compilation errors. Where do these errors occur and what should be done to avoid them?
¿Was it too hard for you to just compile that snipped and read the compiler messages?
The following set of classes raises three compilation errors. Where do these errors occur and what should be done to avoid them?


Errors
1.) pure virtual method must be implemented by derived class virtual void mymethod() = 0;
2.) parameter names for void setparam(int a, int b) are the same as the class members, rename these.
3.) missing closing curly brace after set param.
pure virtual method must be implemented by derived class
As long as you don't instantiate any object of the base or the derived type, there is no problem.
//some more member functions to follow I hope that there is a virtual destructor there.

The x,y variables are innaccessible from the derived class.
As long as you don't instantiate any object
True. I was going by his example. Edit* Actually apparently here I was going off of alastairl's example again. Whoops!

The x,y variables are innaccessible from the derived class.
Good catch, I overlooked this looking at alastairl's code.
Last edited on
closed account (z05DSL3A)
2.) parameter names for void setparam(int a, int b) are the same as the class members, rename these.
That will not cause a compilation error here, however x and y are inaccessible.

Edit 1: just notice ne555 already mentioned the inaccessibility.

Edit 2: I must be having some issues, clanmjcs post wasn't there when I posted.

Last edited on
Topic archived. No new replies allowed.