Click to See Complete Forum and Search --> : Help with some exercises
drdkscully
April 28th, 2002, 03:56 PM
Hello, I have this worksheet to do for my CSCI class. I just need some help on a few questions. I have my own answers to them but I would like some expert help.
1. string *str;
*str= “12345”;
will the above code segment work as expected?
Why?
2. int *k;
char c;
k=&c;
will the above code segment work? Why?
3.class Base{
public:
void public_foo();
private:
void private_foo();
protected:
void protected_foo();
};
class Derived{
public:
void der_public_foo();
void process();
private:
void der_private_foo();
protected:
void der_protected_foo();
};
Base b1, *b2;
Derived d1, *d2;
For each of the following statements, indicate
valid/invalid and if invalid,
explain clearly why.
a) b2 = &d1;
b) d2 = &b1;
c) d2 = &d1;
d2->der_protected_foo();
d) d2 = &d1;
d2->public_foo();
e) b2 = &d1;
b2->der_public_foo();
f) The following is an implementation of
Derived::process()
void Derived::process()
{
Base::protected_foo();
Base::private_foo();
}
4. Write a recursive function to print a decimal
number in reverse order. E.G.: 12345 would be printed as 54321. Note that the input is an integer, not a string. Hint: Use the / and % operators.
Enhance the trance,
April
NMTop40
April 29th, 2002, 06:21 AM
you give what you think the answers are and we'll tell you if you are correct, and, if you are wrong, will help to explain to you why.
but we're not going to do this exercise for you. not even for ratings (well some might).
The best things come to those who rate
drdkscully
April 29th, 2002, 03:57 PM
OK, here it goes
1. string *str;
*str= “12345”;
will the above code segment work as expected?
Why? Yes it will work
2. int *k;
char c;
k=&c;
will the above code segment work? Why?
No, you cannot assign a int to a char
3.class Base{
public:
void public_foo();
private:
void private_foo();
protected:
void protected_foo();
};
class Derived{
public:
void der_public_foo();
void process();
private:
void der_private_foo();
protected:
void der_protected_foo();
};
Base b1, *b2;
Derived d1, *d2;
For each of the following statements, indicate
valid/invalid and if invalid,
explain clearly why.
a) b2 = &d1;
Valid
b) d2 = &b1;
Valid
c) d2 = &d1;
d2->der_protected_foo();
Invalid, cannot access protected members
d) d2 = &d1;
d2->public_foo();
Valid
e) b2 = &d1;
b2->der_public_foo();
Invalid, cannot access derived members through
base class
f) The following is an implementation of
Derived::process()
void Derived::process()
{
Base::protected_foo();
Base::private_foo();
}
Invalid, cannot access private and protected
members.
4. Write a recursive function to print a decimal
number in reverse order. E.G.: 12345 would be printed as 54321. Note that the input is an integer, not a string. Hint: Use the / and % operators.
For this one I have no clue!
Enhance the trance,
April
ankursaxena
April 29th, 2002, 04:56 PM
its actually very simple to check all these things, why dont you write small small test programs for each of you questions and see if it works and try and understand, atleast thats how use to do it..its quite simple.
eg.
int main()
{
int* k;
char c = 'a';
k = &c;
return 0;
}
this gives an error and it tells you it can not convert char* to int*..similarly the other...
have fun..
cheers,
Ankur
NMTop40
April 29th, 2002, 05:09 PM
OK, here it goes
1
string *str;
*str= "12345";
In reply to:
will the above code segment work as expected?
Why? Yes it will work
string *str creates a pointer to a string, but does not allocate a string. The pointer is unassigned. Therefore you cannot expect the next statement to work.
2
int *k;
char c;
k=&c;
In reply to:
will the above code segment work? Why?
No, you cannot assign a int to a char
correct, you need to cast.
3 (the comments below are my own)
class Base
{
public:
void public_foo();
private:
void private_foo();
protected:
void protected_foo();
};
//
class Derived // derived from what?
{
public:
void der_public_foo(); // are you serious?
void process();
private:
void der_private_foo();
protected:
void der_protected_foo();
};
//
Base b1, *b2;
Derived d1, *d2;
Can't be bothered continuing after that. Derived does not derive from Base in your example, and none of the functions in Base is virtual, and you cannot override a function by sticking der_ in front of it.
The best things come to those who rate
drdkscully
April 29th, 2002, 08:02 PM
What about the reverse numeric string question? Can someone help me with that??
Enhance the trance,
April
drdkscully
April 29th, 2002, 08:36 PM
class Base{
public:
void public_foo();
private:
void private_foo();
protected:
void protected_foo();
};
class Derived{
public:
void der_public_foo();
void process();
private:
void der_private_foo();
protected:
void der_protected_foo();
};
Base b1, *b2;
Derived d1, *d2;
Can't be bothered continuing after that. Derived does not derive from Base in your example, and none of the functions in Base is virtual, and you cannot override a function by sticking der_ in front of it.
Whoops I am sorry about that. It's supposed to read:
class Derived:public Base{
Enhance the trance,
April
ankursaxena
April 29th, 2002, 08:56 PM
hey!
this might work ok, not the best of way, but should work, havent tested it, but if u want more efficient algo, there might be someone else who might give u one, but as far as i go this works ok for now..
l8r
Ankur
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int val = 8901;
char value1[20] = {0};
itoa(val, value1, 10); //converts int to char array
int sl = strlen(value1); //finds the length of the char array just created.
char x; //this is used for temp storage for swaping
for(int i =0, j=sl-1; i < sl/2; i++, j--)
//we run the loop from 0 to 1/2 of the strlen becuase by then we would have swaped the string.
{
//general swaping
x = value1[i];
value1[i] = value1[j];
value1[j] = x;
}
int val2 = atoi(value1);
//converting the reversed string back to int.
printf("%d", val2);
//printint it
output : 1098
ankursaxena
April 29th, 2002, 09:18 PM
hey this might work 2, if u or ur teacher doesnt want to use strings..
Ankur
int val = 98363456;
int rem = 0;
int newval = 0;
for(int i=0; i<10; i++) //10 becuase thats the max number length of an int. i am assuming 32 bit int.
{
rem = val % 10; //get the last digit.
newval = newval*10 + rem; //add it to the newval after multiplying the new val by 10 to **** its position forward.
val = val / 10; //get the remainig int value
if(val == 0) //if val becomes 0 then break out.
break;
}
printf("\nnewval = %d\n", newval);
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.