Base Class Pointer

No. Say I have three Classes. Employee, Hourly Employee and Salaried Employee. Hourly and Salary are both inheritied from Employee. In C/C++ you can create a pointer to Employee:

Employee * tmpEmp;

and use it to reference objects of both Hourly and Salaried:

tmpEmp = tmpHourlyEmp;
tmpEmp = tmpSalariedEmp;

I checked out the MSDN library, but the closest I came to were delegates. So, any ideas?
 
Delegates have nothing to do with that.

You can declare a variable of the type of your base class and assign an instance of any derived class to it, but calling the methods on it will still result in the derived methods being called.
 
Actually, you can't. If that would have been the case I wouldn't have posted. When I tried to assign an Hourly Employee instance to an Employee instance, the employee instance did not change. I could not access the data or methods of the Hourly Employee instance.
 
I'm not sure what you want, Diesel...

If you declare a variable as Employee and set it equal to an instance of HourlyEmployee, of course you can't access the HourlyEmployee's members (if they're specific to HourlyEmployee). On the other hand, if HourlyEmployee overrides a method in Employee and you try and call the method on your Employee variable, it will STILL call the HourlyEmployee's version (even though the variable is of type Employee). That's not really what you asked, but I was clarifying what divil said.

I think your original question was how do you assign a variable to the base class's type, which you've already done as soon as you did:
C#:
HourlyEmployee hEmployee = new HourlyEmployee();
Employee emp = hEmployee;

The second line is what you asked for, no?

-Nerseus
 
I know what you two are saying...I tried that approach at first, and it doesn't work. As in the previous example, say if you had a variable HourlyPayRate in the HourlyEmployee class. Even after you set the Employee emp instance equal to the hEmployee instance, you still cant access that data member. I don't think you can assign derived classes to base classes in .NET.

Thank you for your help though.
 
Once more, since you don't seem to understand:

C#:
HourlyEmployee hourEmp = new HourlyEmployee();
Employee emp = hourEmp; //Ok!

emp.SomeMethodWhichIsPartOfEmployeeClass(); //Ok!
emp.SomeMethodWhichIsPartOfHourlyEmployeeClass(); //Bad!

((HourlyEmployee) emp).SomeMethodWhichIsPartOfHourlyEmployeeClass(); //Ok!

I hope you can see the difference here. By casting an HourlyEmployee to type Employee you can only call those methods which were inherited from Employee, you cannot call the 'new' HourlyEmployee methods without first casting back to HourlyEmployee. This is a design paradigm of OOP and is reflected in all languages. Can you show an example of when this isn't the case?

To use a different example, imagine the 'thoughts' (for lack of better term) of the compiler when it hits this line:

C#:
emp.SomeMethodWhichIsPartOfHourlyEmployeeClass();

//Compiler thinks:
//hey! emp is an Employee, but there's no such method for Employee!
//It doesnt KNOW that this Employee is actually an extended
//class with that method, all it knows is that it is dealing with an
//Employee and Employees cant do that!

If you're still not convinced, please post the code which compiles in C++ but which cannot be reproduced in .Net.

:)
 
Last edited:
Haven't checked the boards in a few days...but, what Im talking about is Polymorphism....

#include <iostream>
using namespace std;

class item {

public:
float getprice() {return price; }
int getquantity() { return quantity; }
virtual int getounces();
virtual void setounces(int);


private:
float price;
int quantity;

};


class pepsi: public item {

public:

int getounces() { return ounces; }
void setounces(int oz) { ounces=oz;}

private:

int ounces;

};



void main() {

item *someitem;
someitem = new pepsi;

someitem->setounces(20);
cout << someitem->getounces;

}
 
Really not sure what your problem is with this I think VolteFace and Squirm explained it clearly but here you go:

C#:
using System;

namespace ConsoleApplication1
{

abstract class item 
{

public float getprice() {return price; }
public int getquantity() { return quantity; }
public abstract  int getounces();
public abstract  void setounces(int oz);


private float price;
private int quantity;
};


class pepsi: item 
{
public override int  getounces() { return ounces; }
public override void  setounces(int oz) { ounces=oz;}

private int ounces;
};


	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			item someitem;
			someitem = new pepsi();

			someitem.setounces(20);
			Console.WriteLine(someitem.getounces());
		}
	}
}
 
Last edited:
Imports System

Namespace ConsoleApplication1

MustInherit Class item

Public Function getprice() As single
Return price
End Function
Public Function getquantity() As Integer
Return quantity
End Function
Public abstract Integer getounces()
Public abstract void setounces(Integer oz)


Private price As single
Private quantity As Integer
End Class



Class pepsi
Implements item
Public Overrides Function getounces() As Integer
Return ounces
End Function
Public Overrides Sub setounces(ByVal oz As Integer)
ounces=oz
End Sub

Private ounces As Integer
End Class



'/ <summary>
'/ Summary description for Class1.
'/ </summary>
Class Class1
'/ <summary>
'/ The main entry point for the application.
'/ </summary>
<STAThread> _
Shared Sub Main(ByVal args() As String)
Dim someitem As item
someitem = New pepsi()

someitem.setounces(20)
Console.WriteLine(someitem.getounces())
End Sub
End Class
End Namespace

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------


This may not be a perfect convert to VB.Net, but it should get you started. BTW, There are a ton of c# to vb.net converters online.
 
Back
Top