Saturday, May 9, 2009

Can someone help me with this C++ Inheritance problem?

#include %26lt;cstdlib%26gt;


#include %26lt;iostream%26gt;





using namespace std;





int main(int argc, char *argv[])


{


class Printer {


protected:


int level;


public:


int getlevel();


};





int Printer::getlevel() {


return level;


}





system("PAUSE");


return EXIT_SUCCESS;


}





Why doesn't that work?

Can someone help me with this C++ Inheritance problem?
You can't put member function definitions from nested classes outside the class declaration, at least not with Dev-C++. I'm not sure if any compiler would support that. If you defined the getlevel function right after the declaration it would work:





class Printer {


...


int getlevel() { return level; }


...


};


...
Reply:You should use that protected varible within class and subclass of that class. then only it works.


public: within that class and outside of the class


protected:within that class and subclasses of that class


private:within that class only and cannot be accessed anywhere except within the class.


In c++,INHERITANCE helps in making a general class into a more specific class.?

is the statement true or false?state with reasons.

In c++,INHERITANCE helps in making a general class into a more specific class.?
Search google for C++ Inheritance, you should find a pretty clear definition. Plus you get the added benefit of doing your own research ;-) . This should also be in your class text book, check out the index in the back. look for "inheritance". Now if there was something particular about Inheritance you didn't understand, i'd gladly help.
Reply:Sorry, but I can't do your homework for you unless I can receive the "A" for doing it.





Why not read the text book for your class? OR, if you mail it to me, I will read it FOR you, and then send the book along with the answer back to you - at your expense, of course.





But in either case, good luck and enjoy your class.
Reply:i think its TRUE because for example


a class with data member name is general;


but its derived class with names of students,teachers,etc.


is a more specific one.

online survey

Urgent! help in c++ inheritance?

add= a+b+c using inheritance


and then the answer will be saved on drive c:\





pls help me create a program

Urgent! help in c++ inheritance?
I don't mind helping with code, but can you please at least *try* to do your homework?





Edit: Look, post some code. This one isn't that hard. If you're feeling stuck, maybe you just have to walk away from it for a bit and come back.
Reply:What is a,b,c class or integer or something else your question is not clear.


C++ Inheritance help!?

can anyone make a C++ program with the use of inheritance?





and with the use of this formula:





P=I/T

C++ Inheritance help!?
what do you mean by P = I/T? and what do you want to code using inheritance? if you need examples using inheritance search the net for it


C++ Inheritance?

Hi guys,


Can you please elaborate the statement below.. it's about C++ Inheritance...





"Including a base-class member function with a different signature in the derived class hides the base-class version of the function. Attempts to call the base-class version through the public interface of a derived-class object result in compilation errors."





Thanks guys.

C++ Inheritance?
Okie i am into JAVA so i would call it





*Method overriding*





Method(Function) overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.





The new method definition must have the same method signature (i.e., method name and parameters) and return type. Only parameter types and return type are chosen as criteria for matching method signature. So if a subclass has its method parameters as final it doesn’t really matter for method overriding scenarios as it still holds true. The new method definition cannot narrow the accessibility of the method, but it can widen it. The new method definition can only specify all or none, or a subset of the exception classes (including their subclasses) specified in the throws clause of the overridden method in the super class





A static method cannot be overridden to be non-static instance method as shown in the overridden method declaration of method9. A static method is class-specific and not part of any object, while overriding methods are invoked on behalf of objects of the subclass.





A subclass cannot override fields of the superclass, but it can hide them. Code in the subclass can use the keyword super to access members, including hidden fields.





The following distinction between invoking instance methods on an object and accessing fields of an object must be noted. When an instance method is invoked on an object using a reference, it is the class of the current object denoted by the reference, not the type of the reference, that determines which method implementation will be executed. When a field of an object is accessed using a reference, it is the type of the reference, not the class of the current object denoted by the reference, that determines which field will actually be accessed. This is demonstrated in the above program.











Sorry i dint sticked to C++....yet i am sure Concept is almost the same.





hope this helps


Cheers:)
Reply:Not too sure about C++ but this is the situation with C#.





The compiler gives a "'AnswersDerivative.SampleMethod()' hides inherited member 'Answers.SampleMethod()'. Use the new keyword if hiding was intended." warning with the following code. But the derived class will implicitly hide the base method, however there is no problem with the method with the alternate signature because it doesn't match anything in the base class





public class Answers


{


public Answers()


{


}





public void SampleMethod()


{


}





}





public class AnswersDerivative : Answers


{


public /* new */ void SampleMethod()


{


}





public void SampleMethod( int AlternateSignature )


{


}





}
Reply:hey this is called function overriding


u have to use VIRTUAL keyword before the base class's function, otherwise if u make an object of derived class %26amp; call its' function, then even base class's function will be called.





class base


{


public:


virtual void show()


{cout%26lt;%26lt;"Base Class";}


}





class derived:base


{


public:


void show()


{cout%26lt;%26lt;"Derived Class";}


}





void main()


{


base obj1;


obj1.show() // Base Class function will be called


derived obj2;


obj2.show() // Base class function will be called if u don't put VIRTUAL keyword before the function defined in base class


}