| View previous topic :: View next topic |
| Author |
Message |
Qvazzler Advanced Cheater
Reputation: 0
Joined: 02 Jan 2007 Posts: 68
|
Posted: Sun Oct 21, 2007 5:30 am Post subject: C++ Test 3: Inheritance/Object-Orientation |
|
|
Might aswell skip the second test, I underestimated alot of people on here.
Also, don't go on thinking it's my homework that needs to be done again. If I would want the right answers asap I would go to someone else. Fortunately I had this test last friday and I aced it, so I guess most of you pro's will be able to ace this one aswell. I hope it hasn't been too rubbishly written, I have to translate it from swedish after all.
1. What do you call a variable created by a class?
2. What is true about reachability (internally) of private attributes that a class inherits from?
- The class that inherits cannot have any attributes
- It's not possible to inherit from a class with private attributes
- The class that inherits can only reach public attributes
- The class that inherits can reach all private attributes
- the inheriting class can only read private attributes that are variables
- a class cannot have private attributes at all
3. Describe the term "Base class" (this is used as a professional term)
4. Describe the term "Sub class" (this is also used as a professional term)
5. In a class called 'Color', we have the following constructor:
| Code: |
Color (float Red = 0.0, float Blue = 0.0, float Green = 0.0); |
- Which of the following statements are true? (only 1 is correct)
- It's not possible to create an object of Color because all the
- values are 0
- You can add up to 3 strings as an argument
- You have to use 3 float-types as parameters
- You can use up to 3 float-types as parameters
6. How do you write it so that a class inherits from another class in C++?
- class Banana inherits Fruit
- Banana->Fruit
- public override Fruit : Banana
- class Banana : Fruit
- class Banana extends Fruit
7. We've declared a method called "calculate(int val1, int val2);" in a header-file for our class called "Calculator". What do we have to write to implement this function into the class? Choose the correct alternative:
- int Calculator::calculate(int val1, int val2)
- int Calculator extends calculate(int val1, int val2)
- int Calculator overrides calculate(int val1, int val2)
- int Calculator->calculate(int val1, int val2)
- int Calculator => calculate(int val1, int val2)
- Calculator::int calculate(int val1, int val2)
- void Calculator calculate(int val1, int val2)
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Oct 21, 2007 11:49 am Post subject: |
|
|
1. An object.
2. "The class that inherits can only reach public attributes"
3. Base class is another way of saying 'superclass'. "A class of which another class is an extension, and hence defines properties that are inherited by the other class"
4. The extension class that is used with the base class.
5. "You can use up to 3 float-types as parameters"
6. class Banana : Fruit
7. int Calculator::calculate(int val1, int val2)
|
|
| Back to top |
|
 |
Qvazzler Advanced Cheater
Reputation: 0
Joined: 02 Jan 2007 Posts: 68
|
Posted: Sun Oct 21, 2007 12:02 pm Post subject: |
|
|
Although the correct answer on the second question was:
The class that inherits can reach all private attributes.
That was still
You guys are far more skillful than I am.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Oct 21, 2007 12:03 pm Post subject: |
|
|
| Qvazzler wrote: | Although the correct answer on the second question was:
The class that inherits can reach all private attributes.
That was still
You guys are far more skillful than I am.  |
Really? When I've done some classes setup to inherit others, I couldn't touch the private members. Just as if you tried reaching them normally from a single class.
|
|
| Back to top |
|
 |
Qvazzler Advanced Cheater
Reputation: 0
Joined: 02 Jan 2007 Posts: 68
|
Posted: Sun Oct 21, 2007 12:08 pm Post subject: |
|
|
That's what my teacher told me.. He's been (learning about) programming since he was 8 and he's 25 now. You don't have to take my/his word for it though, as I am lacking good arguments to tell you why you're wrong.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Oct 21, 2007 12:18 pm Post subject: |
|
|
| Qvazzler wrote: | That's what my teacher told me.. He's been (learning about) programming since he was 8 and he's 25 now. You don't have to take my/his word for it though, as I am lacking good arguments to tell you why you're wrong. |
Just tested it again, and yea.. I get told I can't access it:
| Code: | | error C2248: 'SubClass::SubValue' : cannot access private member declared in class 'SubClass' |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sun Oct 21, 2007 1:02 pm Post subject: |
|
|
| It's real easy to access a private member from another class. All you have to do is make sure they are friends.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sun Oct 21, 2007 1:05 pm Post subject: |
|
|
| killersamurai wrote: | | It's real easy to access a private member from another class. All you have to do is make sure they are friends. |
Ah, true. His questions didn't pertain to include anything of friend classes so I didn't even think of them
|
|
| Back to top |
|
 |
TheSorc3r3r I post too much
Reputation: 0
Joined: 06 Sep 2006 Posts: 2404
|
Posted: Mon Oct 22, 2007 3:36 pm Post subject: |
|
|
Wiccaan/Qvazzler: protected is the keyword you're both looking for.
Only a subclass can access its parent class's protected members.
_________________
Don't laugh, I'm still learning photoshop! |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Mon Oct 22, 2007 6:19 pm Post subject: |
|
|
| TheSorc3r3r wrote: | Wiccaan/Qvazzler: protected is the keyword you're both looking for.
Only a subclass can access its parent class's protected members. |
The protected section of a class wasn't what we were talking about. The question at hand was directly focused on the private section of a class.
_________________
- Retired. |
|
| Back to top |
|
 |
nox Expert Cheater
Reputation: 0
Joined: 09 Apr 2007 Posts: 227 Location: brooklyn
|
Posted: Mon Oct 22, 2007 9:47 pm Post subject: |
|
|
what kind of teacher teaches obj-o on the second day
|
|
| Back to top |
|
 |
|