As I develop more and more for a living I have been trying to perfect my developing skills with personal projects in hope to expand my professional career, one of these is the concept of Polymorphism. The last few projects I have worked on, I have this model where I have a TableLayoutPanel, that is a grid of custom UserControls. To try and start reducing redundency coding, I am trying to build base classes of both the Component and UserControl. One part of this is I have a function in the TableLayoutPanel that is used to add the custom UserControls …

Member Avatar
Member Avatar
+0 forum 3

Hi I'm creating a code for my class and what I'm trying to do is to create a triangle class that is derived from abstract class TwoDimShape. However; when I create the triangle class and use base as a variable I get an error. My teacher wants us to use the variable "base" here is my code and the error, any help is appreciated. sing System; public class Triangle : TwoDimShape { private double base; private double height; } and the error is: Error 1 Invalid token 'base' in class, struct, or interface member declaration

Member Avatar
Member Avatar
+0 forum 4

i can understand it when we want to implement polymorphism in c++ we use virtual functions and function overriding but when we want to implement polymorphism in java then we do not use virtual methods why. in java we use simply reference object that can store the address and use function overring and polymorphism implements. Basically my question is that what is the basic difference b/w c++ polymorphism and java polymorphism. plz give me the correct concept about this.

Member Avatar
Member Avatar
+0 forum 1

I'm trying to create a program that passes arguments of size of three shapes, a circle, a triangle, and a rectangle. I got those but everytime I run the code it displays the arguments as 0 instead of giving me the shape dimension. Here is the code. using System; class TestShapeHierarchy { static void Main(string[] args) { TwoDimShape[] myShapes = new TwoDimShape[3]; myShapes[0] = new Rectangle(10, 5); myShapes[1] = new Triangle(12, 6); myShapes[2] = new Circle(8); foreach (TwoDimShape tds in myShapes) { ProcessShape(tds); Console.WriteLine(""); { } } } // you need to write a static method processShape static void ProcessShape(TwoDimShape …

Member Avatar
Member Avatar
+0 forum 1

# Introduction # The subject of this tutorial is a bit more advanced than the subjects I have tackled in the past, but I hope this will find some interested readers. From time to time in C++, programmers find themselves torn apart between using a *Generic Programming* style which primarily involves the use of templates and compile-time mechanisms (e.g., STL), and using a *Object-Oriented Programming* style that often favors inheritance and run-time dispatching mechanisms (e.g., Qt). As it is so often the case, there is no obvious "winner", i.e., there is no paradigm that should always be preferred, even within …

Member Avatar
+2 forum 0

Hello I am trying to create a class that called Lootable and create subclasses such as trunk, locker, etc. that will contain items that can be looted, they will inherit their lootability from the Lootable class. I am thinking of using the ArrayList for this. I have thought of different ways to go about this and have written some code but I am having some difficulties. This is for and assignment and my app idea goes somewhat beyond the requirements of the lesson and no I do not expect someone to due this for me I would however like some …

Member Avatar
Member Avatar
+0 forum 7

I have an abstract class CArticle and two derived classes CBook and CMagazine. I also have a class CLibrary where i want to add articles. Its members are an int variable to count the articles and a double pointer of CArticle. I have the following main function: int main() { CLibrary L1; CArticle *A1=new CBook(1000); CArticle *A2=new CBook(1001); CArticle *A3=new CMagazine(1002,3); CArticle *A4=new CMagazine(1003,6); CArticle *A5=new CMagazine(1004,8); L1.addArticle(A1); L1.addArticle(A2); L1.addArticle(A3); L1.print(); } I can't figure out what type of parameter should my addArticle() function have in order to work for this main. I would like to let the compiler choose …

Member Avatar
Member Avatar
+0 forum 16

I have two containers with the type of *A* A<string, double> s1; A<string, bool> s2; So I wrote the *A* class with the necessary functions and I used a template<class T, class E> for it. It has a function that returns a value of *E*, in my case, it returns a double which I'm trying to compare to another double. I'm supposed to overload the < operator for that, but I have no idea how to do that. 4.5 < s1.at("a"); s1.at("a"); returns 1.5, so it's false. I need it to return true. I thought since they are both double …

Member Avatar
Member Avatar
+0 forum 1

public class GenericDemo<E extends GenericDemo> { E innerE; public E doStuff(E e, GenericDemo<E> e2) { //insert code here } public E getE() { return innerE; } } The options are: 1. return e 2. return e2.getE() 3. return e2 4. return e.getE() Option (1) and (2) compile fine. Option (3) and (4) give compile error. 1. Precisely, for `e2` says, **incompatible types. Found: GenericDemo<E>. Required: E.** My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ? 2. For `return e.getE()` says, **incompatible types. Found: GenericDemo. Required: E.** Here I'm first confused …

Member Avatar
Member Avatar
+0 forum 8

<? super Animal> means animal or it's superclass only. Then why inside the method it's allowing to add Dog object even though it's a subclass of Animal? This program compiles and runs! import java.util.*; class Cat {} class Animal{Animal() {System.out.println("Animal constructor");}} class Dog extends Animal{Dog() {System.out.println("Dog constructor");}} public class GenericDemo5 extends Animal{ public static void main(String r[]) { List l1 = new ArrayList(); //can add anything since no type here l1.add(new Dog()); l1.add(new Animal()); l1.add(30); met(l1); System.out.println(l1); } public static void met(List<? super Animal> l2) { l2.add(new Animal()); l2.add(new Dog()); System.out.println(l2); }} Even with Generic Instantiation(shown below) same output!! This …

Member Avatar
Member Avatar
+0 forum 11

Compiles fine. But at runtime it gives ClassCastException. My doubt is that why is it so even though st which is of type Sample1 gets casted to Sample2 and generates exception even though Sample2 IS-A Sample1.

Member Avatar
Member Avatar
+0 forum 4

class Mixer { Mixer() {} Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String args[]) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.println("hi"); } } The answer to is " hi hi followed by an exception" Here are my doubts: a) When m2 is passed as an arg to Mixer constructor, are we passing the address to the Mixer() object m2 points to? If so, m1 = m will give the same address to m1 and hence …

Member Avatar
Member Avatar
+0 forum 1

Hi all, I know that one advantage of method overloading is that the implementation can be overridden for the same method name, but wanted to know how is that going to be an advantage for us. All the links explain how to do it, and not why to do it and how itself can be an advantage, could someone please explain. Thanks in advance. :)

Member Avatar
Member Avatar
+0 forum 5

So this is mainly a Design/Clean Coding sort of question, I know I've read something about this previously but I can't find it, I believe it's in one of my reference books on C/C++ so it wouldn't likely be useful to my predicament. This question includes a lot of Java code because it seems to be the best way to illistrate what I'm trying to do. I'm working on a program in Java (so no Operator overloading, which the book I mentioned used). I'm working to isolate the types of data from a buffer class I'm trying to make. The …

Member Avatar
Member Avatar
+0 forum 2

> I'm implementation polymorphism and have one confusing that if I use instanceof then I'm trying to perform casting which voids polymorphism so will that be any way to avoid instanceof with the code and still have the program working? Block of code where I have used instanceof starts from line , also if anyone can point out if there is any redundant code here. Any help is much appreciated. import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; //implements class Orders from Constant public class Orders implements Constants { //Declaring array private static Rocks[] rocksArray; private static int numOrders; /** * @param …

Member Avatar
Member Avatar
+0 forum 2

I'm working on a program that demonstrates polymorphism. It asks the user to create an elementary, high school, or college student. Once a student is created, it is stored in an array list. When prompted, the program is suppose to print out each of the student's information. How do I go about printing this information, and making sure the appropriate printString methods are called? Main Class: import java.util.ArrayList; import java.util.Scanner; // Student Application public class StudentApp { private static ArrayList<Student> students = new ArrayList<Student>(); public static void main(String[] args) { String studName, schoolName, teachName, counselor, major, location; double gpa; int …

Member Avatar
+0 forum 0

Hi all, I have started finding lots of uses for abstract classes to define interfaces for common functionality using polymorphism and i know that using polymorphism and virtual functions incurrs some additional call cost of functions. So what im wondering is can i get the best of both worlds? consider the following basic example [code] class iShape { public: iShape(); virtual ~iShape(); virtual float GetArea( void ) = 0; virtual 3dPos GetPos( void ) = 0; }; [/code] Using an abstract class like this gives me a good interface and hides the implimentation and all that stuff and i really …

Member Avatar
Member Avatar
+0 forum 3

HELP ME PLEASE. I AM A 1ST YR COLLEGE STUDENT :/ Define an interface Filter as follows: public interface Filter { boolean accept(Object x); } Modify the implementation of the DataSet class to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by having a data set process a collection of bank accounts, filtering out all accounts with balances less than $1,000. Refer to the source code below: [CODE]/** Computes the average of a set of data values. */ public class DataSet { private double sum; private Object maximum; …

Member Avatar
Member Avatar
+0 forum 12

[CODE]#include <iostream> using namespace std; class bike { public: bike () { cout << "Bike: no parameters\n"; } bike (int a) { cout << "Bike: int parameter\n"; } void color(int a,int b) { cout<<"Inside Bike Color"<<endl; } }; class ducati : public bike { public: ducati (int a) { cout << "Ducati: int parameter\n\n"; } void color(int a,int b,int c) { cout<<"Inside Ducati Color"<<endl; } }; class honda : public bike { public: honda (int a) : bike (a) { cout << "honda: int parameter\n\n"; } }; int main () { ducati monster(0); ducati *dObj=new ducati(10); bike *bObj=&monster; bObj->color(10,20,30); //it's …

Member Avatar
Member Avatar
+0 forum 1

Ok I am a programming student and need help with a problem, I already have most of it (so I don't need you guys to do my hw :P) but I am having problems with am, and I am not even sure if I am doing it right. [B]Problem:[/B] (The Person, Sruden, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define …

Member Avatar
Member Avatar
+0 forum 6

Hey all:) I'm learning Java and I'm having a little trouble on a polymorphic write-to-file function. Let me quickly explain my project: I have an abstract class Mammal, with non-abstract children Horse, Cow etc. I have a Main program that creates a couple Horses and Cows, and store these in a Mammal array. Now the Mammal class and its children have a function called WriteData(), and the plan is that in my Main program I should be able to call this function polymorphic like array[index].WriteData(), and each Horse and Cow should write their personal info on a separate line to …

Member Avatar
Member Avatar
+0 forum 2

Hello. I'm currently making a program that deals with a priorityQueue and inheritence/polymorphism. Student -> undergrad -> coop_student or student -> grad Now, the queue takes in the data and just puts it in a linked list and gives it a priority. That way, there are 3 queues and we dequeue from the first going to last. Here are the snippets that have the problem. Note: I was told that I don't need an operator=, but if I do, I can add one. priorityQueue.cpp:30: error: no match for ‘operator=’ in ‘n->node::data = theStudent’ [CODE]void priorityQueue::enqueue( student * theStudent, int priority …

Member Avatar
Member Avatar
+0 forum 3

Hey guys i'll show the code first then explain my problem: [code=c] Class A { /*Variables required by A, B, C and D */ /*functions required by B and C*/ }; Class B { /*some of it's own functions and variables*/ }; Class C { /*some of it's own functions and variables*/ }; Class D { /*calls all the functions in A, B and C*/ }; [/code] So basically, I need the above and wondered what would be the best system design. I was thinking of using virtual functions with inheritance but then I come with the issue that if …

Member Avatar
Member Avatar
+0 forum 18

Hello all I have a query,may be silly but i have to clear it.IT IS REGARDING THE UPCASTING.I want to "when we can invoke the methods of base class using the object of derived class.then what is the need of assigning the subclass object to base class reference." I mean what is advantages of using that reference in comparison to using subclass object.

Member Avatar
Member Avatar
+0 forum 5

Basically this program uses an abstract class which 3 other classes extends to so that in the end an overridden toString() method will print out how many pages must be read for each class. Okay, well this program is for a class, but I am kinda of stuck because my teacher wants me to test the Homework methods below with ArrayList, but I cannot reference is from a static context.My teacher wasn't specific about the tester. All he said was to use Arraylist to test the classes. So I assume it was 4 different arraylist because there is 4 different …

Member Avatar
Member Avatar
+0 forum 3

I need help with an assignment for my programming course. I have three classes A,B,C. all stored in one array. (These are not my real classes there are just a guide) [CODE=c] Class A{ char name; public: virtual print(){ printf("%s",name); } } Class B : public A{ char middleName; public: print(){ A::print(); printf("%s", middleName); } } Class C : public B{ char lastName; public: print(){ printf("%s",lastName); } } /*I wish to call the print from all the classes, assume this array is full of a variaty of A,B,C */ A* classes[20]; classes[0]->print(); classes[1]->print(); ........ [/CODE] This works fine for classes …

Member Avatar
Member Avatar
+0 forum 10

I have a design question which I have not been able to find a satisfactory answer for by searching. Essentially, I have a class - Base - that contains a vector of pointers to another class - Node. However, I would like to be able extend both the Base and Node class. This will involve adding new variables and functions to the classes derived from Node, and utilising these additional functions in classes derived from Base. I can think of a few ways to accomplish this, but am unsure which is the best from a design point of view, as …

Member Avatar
Member Avatar
+0 forum 9

Hi, just a quick question (i hope!!), i'm working on an assignment where i need to build a user-defined library of shapes. The class structure is to have "shape" as a base-class with both 2d and 3d shapes as derived classes. I've handled this by storing them in a vector template (though i've omitted this in the code below for simplicity). One of the main requirements is to pass a 2D shape derived class object (e.g. "circle ci1") as an argument in a generic derived class known as "prism" that will take the area of the 2d shape and allow …

Member Avatar
Member Avatar
+0 forum 7

Hello Everyone, I'm trying to implement a Polymorphic Queue. Here is my trial: [CODE]QQueue <Request *> requests; while(...) { QString line = QString::fromUtf8(client->readLine()).trimmed(); if(...)){ Request *request=new Request(); request->tcpMessage=line.toUtf8(); request->decodeFromTcpMessage(); //this initialize variables in request using tcpMessage if(request->requestType==REQUEST_LOGIN){ LoginRequest loginRequest; request=&loginRequest; request->tcpMessage=line.toUtf8(); request->decodeFromTcpMessage(); requests.enqueue(request); } //Here pointers in "requests" do not point to objects I created above, and I noticed that their destructors are also called. LoginRequest *loginRequest2=dynamic_cast<LoginRequest *>(requests.dequeue()); loginRequest2->decodeFromTcpMessage(); } }[/CODE] Unfortunately, I could not manage to make work Polymorphic Queue with this code because of the reason I mentioned in second comment. I'm open to any improvement of my …

Member Avatar
Member Avatar
+0 forum 7

Hello! Im having a problem with the following code structure. The code compiles without problems but when i start the program i get an "exception: __non_rtti_object at memory location...". In the constructor of the model-class i still can get the object information of the node-class through the base pointer. Once the construction of the model-object is completed the class information somehow got lost. I would be very grateful if somebody could point the problem out. Thanks! [CODE] #ifndef PATH_H #define PATH_H template<class T> class Path { private: T* lastPathComponent; Path(const Path<T>&); Path<T>& operator=(const Path<T>& rv); public: Path(const T& singlePath) { …

Member Avatar
Member Avatar
+0 forum 3

The End.