{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Apr4&7.inheritance2

Apr4&7.inheritance2 - Managed vs Unmanaged classes...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Managed vs. Unmanaged classes Last time we saw a banking example that is a standard C++ class hierarchy Let's change things so that it uses a set of CLR classes To do this we'll just insert the keyword ref in the class definitions – and what do you think will happen? Well … we get a bunch errors – 20 of them, to be exact, and they all look something like this: error C3222: 'AcctNum' : cannot declare default arguments for member functions of a managed type or generic functions So we get rid of all the default arguments in the constructors To do this, we make constructors that give explicit values to the data members. Then we compile again – What happens now? We get some more errors 3 of them – they look something like this: error C2512: 'Checking' : no appropriate default constructor available
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Why did we get these errors? Error C3222 happened because we had code that looks like this: Checking (int AcctNum = 0000, int custID = 000, double Bal = 0, double Min = 1000, double Chg = .5); Default values for arguments are not allowed for managed types or generic functions Error C2512 happened because we defined a constructor with an argument for each data member but did not define a no-argument (default) constructor for the classes This is a problem because when an object of a derived type is declared, here is what happens: Default constructor for the basest base class is called Default constructor for the next-base class is called, etc. This means that when a Checking object or a Savings is declared, the default constructor for BankAccount should be called and there isn't one Similarly, when a SuperPlus object is declared, the default constructor for Checking should be called and there isn't one
Image of page 2
Managed vs. Unmanaged classes If we are working in the CLR, there are some issues that are significant: an unmanaged type cannot derive from a managed type a ref class can only inherit from a ref class or interface class the bottom line is that you need to keep a hierarchy compatible: all standard C++ or all CLR Working with managed types: no default arguments are allowed for member functions of managed types or generic functions this means that you have to make a default constructor that explicitly assigns the default values for data members instead of using the default argument syntax of standard C++ it also may mean that you need some extra constructors for situations in which you may want to do some default, some supplied data member values (e.g. accounts and values in the banking example)
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Multiple Inheritance So far, we have just been dealing with single inheritance – the inherited class members can be traced back to a single base class Multiple inheritance occurs when a class is derived from more than one base class – multiple inheritance is more complex and error prone that single inheritance what happens if both base classes have the same data member name?
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern