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:
...
};
#include <iostream>
usingnamespace std;
//this lets the compiler know that the func exists
longint mypow(int m, int n);
int main ()
{
cout<<mypow(8,6);
getchar();
}
longint mypow(int m, int n)
{
longint ReturnValue=(longint)m;
for (int Itter=0;Itter<n-1;Itter++ )
{
ReturnValue*=(longint)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;
}
};
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?
class MyClass1
{
private:
int x; int y;
public:
virtualvoid 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;
};
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.
#include <iostream>
usingnamespace 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();
}
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
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 virtualvoid 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.