Solved

Inheritance in C++

Posted on 1998-10-26
11
247 Views
Last Modified: 2010-04-02
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
Comment
Question by:Gideon
  • 4
  • 3
  • 2
  • +1
11 Comments
 

Author Comment

by:Gideon
ID: 1176253
Edited text of question
0
 

Author Comment

by:Gideon
ID: 1176254
Please help me urgently . I have got to submit this assignment.
0
 
LVL 3

Expert Comment

by:arnond
ID: 1176255
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176256
Did that help?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1176257
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:arnond
ID: 1176258
RONSLOW, I thought that we at EE weren't suposed to help pupils with their school homework. (I'm sure you know this)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1176259
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
 

Author Comment

by:Gideon
ID: 1176260
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176261
>>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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 80 total points
ID: 1176262
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
 

Author Comment

by:Gideon
ID: 1176263
Adjusted points to 85
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now