• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Inheritance in C++

How do you declare a base class "LIST"? (what are its methods and attributes?) The subclasses,Point,Line and Rectangle inherits the methods and attributes of this super class. Think of these shapes as a list of N Point ( using putpixel function to draw a point)
0
Gideon
Asked:
Gideon
  • 4
  • 3
  • 2
  • +1
1 Solution
 
GideonAuthor Commented:
Edited text of question
0
 
GideonAuthor Commented:
Please help me urgently . I have got to submit this assignment.
0
 
arnondCommented:
I think you mean 'template' so here goes:
from the VC++m on-line help:

template
-------------

C++ Specific ®

template < [typelist] [, [ arglist ]] > declaration

The template declaration specifies a set of parameterized classes or functions.

The template parameter list is a comma-separated list of types (in the form class identifier or typename identifier) or a non-type to be used in the template body. The declaration field must be a declaration of a function or class.

You can instantiate a class template much like you would instantiate a normal class, but include the template arguments within angle brackets. No special syntax is required to call a function template.

See Also   Template Topics

END C++ Specific

Example

// Example of the template keyword
template <class T, int i> class TestClass {
public:
   char buffer[i];
   T testFunc(T* p1 );
};

template <class T, int i>
T TestClass<T,i>::testFunc(T* p1) {
    return *(p1++)
};

// To create an instance of TestClass
TestClass<char, 5> ClassInst;


Hope this helps,
Good luck in your assignment,
Arnon David.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
nietodCommented:
Did that help?
0
 
RONSLOWCommented:
class LIST {
  POINT* m_pPoints;
  int m_nPoints;
public:
  LIST()
    : m_pPoints(NULL)
    , m_nPoints(0)
  {
  }
  virtual ~LIST() {
    SetSize(0);
  }
  void SetSize(int n) {
    // if n == m_nPoints return
    // delete old array with delete[]
    // allocate new array with new
    // set m_nPoints accordingly
    // NOTE: this does not let you keep points when
    // changing the size
  }
  void SetAt(int i, POINT p) {
    m_pPoints[i] = p;
  }
  void Draw() const {
    for (int i = 0; i < m_nPoint; i++) {
      putpixel (m_pPoints[i]); // depends on impl of putpixel
    }
  }
};

eg.
class Rect : public LIST {
  void Set (int x1, int y1, int x2, int y2) {
    POINT p0 (x1,y1);
    POINT p1 (x1,y2);
    POINT p2 (x2,y2);
    POINT p3 (x2,y1);
    SetSize(4);
    SetAt(0,p0);
    SetAt(1,p1);
    SetAt(2,p2);
    SetAt(3,p3);
  }
};

0
 
arnondCommented:
RONSLOW, I thought that we at EE weren't suposed to help pupils with their school homework. (I'm sure you know this)
0
 
nietodCommented:
we are supposed to HELP them, we aren't supposed to DO their work.  This seems a bit borderline to me.  He gave a very complete example, but only of part of the project.  The student has to apply that to the rest.  Personally, I wouldn't have done as much, but I think its just a judgement call.
0
 
GideonAuthor Commented:
Arnord is completely off target,may be he did not understand the question.This is my first time to use expert-exchange facility and i did not expect such disfunctional behavior from Arnord.I did not ask him to do the assignment for me, i merely asked for help so that i can DO my own work.Nietod,thanks for the comment and Ronslow thanks for your HELP.How do i transfer the point to you??Arnord,you started at this level as well,next time it will be your brother.Do you expect me to respond the same way??
0
 
nietodCommented:
>>Arnord is completely off target,may be he did not understand the question
I think he was trying to pinpoint the area that was giving you the difficulty so he wouldn't provide too much help.  Apparently he missed.  But if that happens in the future, you an always add a comment explaining the situation and/or asking the export for more help.  That is why I added that "Did that help?" comment.  

>>.How do i transfer the point to you
RONSLOW will have to submit a "dummy" answer.  Then you can grade it.  He is in Australlia so he's probably sleeping now.
0
 
RONSLOWCommented:
I'm awake now !!

Arnond ... I didn't think my answer was anywhere near 'complete' enough to be considered doing his homework for him.  I just gave him a starting point (only the easy stuff).  The main work is in the bits I've left out (and in debugging and understanding what I _did_ put up there).

Like all ethical experts here (such as yourself), I will _not_ do someone's assignment for them .. but a nudge (or push) in (hopefully) the right direction is generally accepted as OK.  There are _some_ experts who don't agree with this and will quite happily help someone cheat.

Roger

PS: Gideon .. I am _not_ suggesting that you are cheating (or attempting to), but there _are_ many students who simply post their assignment questions verbatim and expect us experts to answer them ... these sort of questions usually stand out like a sore thumb and usually get appropriate responses ... much less helpful than Arnond's :-)

0
 
GideonAuthor Commented:
Adjusted points to 85
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now