Solved

how to apply the class

Posted on 1998-05-07
18
249 Views
Last Modified: 2010-04-10
Dear Sir/Madam:
  After two weeks's study,I have erect a preliminary
concept the class£¬but in practical problems,how
to apply the class?Can you tell me some experience?
Thank You!
0
Comment
Question by:youngest
  • 7
  • 4
  • 4
  • +2
18 Comments
 
LVL 1

Accepted Solution

by:
dreamPeace earned 100 total points
Comment Utility
  1. U can give me a problem and I'l show u how it's applied there.
   2. I can show u a real-life (my own) problem, but u need to know basic TCP/IP and
      multi-processing.
   3.
           Let's say u have a class describing an application (like in Java).
           First, the application needs to start up and build it's menu's and load preferences.
           That's the job of the constructor (usually it only allocates memory).
           When it's done, it needs to save files. That's the destructor.
           It also needs a function to return it's name so Windows can identify it.
           Finally, it needs a function to be called when the programm is ready to run, where
           initialization has ended. That's another function.
              Now consider that a user have ran two instances of Netscape. Netscape, is
           in fact a Visual C++ class, which now, has two instances or objects.  Each needs
           it's own intialization function, it's own run function and - it's own memory to store
           HTML's and other data like the current page.  U have a class, u have two objects,
          and it's real.
                 I assume that's what  u wanted, if not, let me know what did u.
0
 

Author Comment

by:youngest
Comment Utility
Thank you for answering carefully.
I will submit you a problem:
 I want to draw two rectangles,I will call the function
about palette so i can fill it color,but the color has a range,for example,the red ,it has kinds of with the different proportion  R,G,B,it has different "RED"
I have written  the prognam in c for dos,now ,I want
to rewrite the prognam in VC++,so i can add some resource to it,Can you help me? how to apply the "class"?
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
A class is just a combination of some data and the functions that apply to it.

A class includes a constructor that initializes the data for each object of that class, and a destructor that cleans up when an object is destroyed (eg. a local variable holding the object goes out of scope at the end of a function).  If you don't write a constructor or destructor, the compiler will generate one for you if possible.

A class usally also defines methods (functions) that provide users of a class object with information about the object (eg. rectangle colour) and also allow it to change that information object.  It also would have methods for telling the object to do something (eg. drawing itself).

In your example, one would assume you have a class for a rectangle.

The rectangle class would have member variables for the rectangle coordinates and/or dimensions and the colour.
eg.
class MyRectangle {
  int x, y, width, height;
  COLORREF color;
  ...
};

The recantgle class would know how (ie have member functions) to set/get the coordinates, dimensions and colour of the rectangle.

eg.
class MyRectangle {
  ...
  void SetColor (COLRREF newcolor) { color = newcolor; }
  COLORREF GetColor() const { return color; }
  ...
};

It would also have a member function (or method) for drawing itself.  The drawing method would set the approriate line and fill colours and then draw the rectangle at the approriate coordinates.

class MyRectangle {
  ...
  void Draw ( ... ) {
    // do the drawing
  }
};

How the drawing method works depends on your graphics library.  For example, under Windows, you'd pass the drawing method a device context that corresponds to a window, and the drawing method would create a pen and brush, select them into the context, then call the grphiacs function to draw the rectangle and finally restore the device context.

I don't understand what you mean by 'but the color has a range,for example,the red ,it has kinds of with the different proportion  R,G,B,it has different "RED"'.  Can you please explain this more carefully .. perhaps giving an example.

I hope this helps you.  Perhaps you could post some of your code and I could help you further.

0
 

Author Comment

by:youngest
Comment Utility
Dear ronslow:
  Your answer is very excellent.I accepted it gradly.
"the color has a range"means,for a instance,in turbo c
,we used function setcolor(RED),set the palette.it can
only express a single'red'colour,it can't express other
"red",eg,pink.also,as we all know,whatever colour,
it is composed of three basial colours:RED,GREEN,BLUE,IN BC++,there is a function to illustrated it .RGB(RED,GREEN,BLUE),we add the value of G,B,decrease the RED's,the red's degree will decrease.The apply will be write a CAI prognam,welome to give me some good advice,
thank you!

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Most systems use eitehr color indices (eg 16-color system) or RGB triples for specifying colour.  In Win32 the datatype to hold an RGB triple is called a COLORREF.  There is an RGB(r,g,b) macro to create a COLORREF from the individual reg, green and blue components.  Similarly there are macros GetRValue, GetGValue and GetBValue that extract the r,g,n values from a COLORREF.  Win32 can transparently map RGB (COLORREF) triples to a color index if the display is 256 colors or less .. so you can just work in RGB and WIn32 will do the rest for you.

How does the various shades of red affect the application of you class. ?

PS: If you want to accept my answer, you need to reject the current proposed answer so that I can post mine as an alternative that you can grade.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Although RONSLOW's answer is very good.  A few minor points of dissagreement.  He says that a class is

" just a combination of some data and the functions that apply to it."

That tends to be true.  But some classes are just data, that is no functions.  And some classes are just functions with no data.  

He says that
"If you don't write a constructor or destructor, the compiler will generate one for you if possible."

I think "if possible" is the wrong way to look at it.  It creates a constructor or destructor if necessary. Constructors  or destructors are always possible (except if there is no default constructor for a base or data member).  But the compiler won't create one if it doesn't NEED to.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Todd and Roger, ctors and dtors are always created if the user does not supply them.  A default ctor will call the ctors of base classes and data members and initialize the vtable (if needed).  The default dtor will likewise call the dtors and destroy the vtable.  If there are no base classes, no virtual member functions and the data members are all either built-in types or missing, the generated ctor and dtor will be empty and can be optimized away.
Things get more interesting when talking about copy ctors (which are automatically generated too).
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
You are garanteed that there will be no Ctors and Dtors for POD (plain old data).  POD (an official term) is a simple type, like enum's, integers, or floating points.  POSs can also be classes whose base class (if any) is POD, whose members are all POD, who contains no virtual functions, and who has no user defined default constructor and destructor.  You are garanteed that a POD class will have no constructor or destructor.

A Ctor will be created only if the compiler detects a reason for it, like it has a base class or member that needs to be constructed or if it has any virtual functions (so it can set the virtual function table pointer.)  Same with destructors.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
Hmmm...  OK.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
nietod .. you said I was wrong and then agreed with me :-) .. you said its always possible (except if there is no default constructor for a base or data member).  So they ARE only generated if possible, and if you don't supply your own.

Of course, you subsquent detail on POD, POS (or aggregates) is very good ... nut I didn't want to go into that level of detail and complexity .. ot seems our poster doesn't understand fully what a class is or how to use one.  I think an explanation of the circumstances under which a default constructor is generated is a bit beyond what is requird and may obscure the more basic question, and confuse our poor poster.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I don't think I agreed.  This is splitting hairs, but the way you worded it you seemed like the constructors would be created if the compiler COULD create them, not if it NEEDED to create them.  It always CAN create them (although may do nothing) so you explanation sounded (to me) like the constructors/destructors would always be created.  But they aren't always needed, so they aren't always created.  And that is garanteed, that is not implimentation defined.  
0
 

Author Comment

by:youngest
Comment Utility
Thank you for help again.from the above debate,I think the class is likeness to struct .but Are there some difference about the allocating of the memory?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
A class is 100% like a structure with one (insignificant) difference.  A class defaults to having private members and private inheritance.  A structure defaults to having public members and public inheritance.  This is only the default, in both cases you can control what members are public and what members are private so the difference is not very important.  You can make a structure that wokrs 100% like a class or a class that works 100% like a structure.  
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
I think 'youngest' meant to say "I think a class is like a C struct".

Yes .. it is, but it also lets you define the functions that operator on the struct, as well as control the access to them (eg. can a variable only be accessed from one of the classes memeber functions).

Also classes allow you to have more control over how their members are initialized and destroyed, and how they are to be copied (if at all).

C++ structs are (as nietod correctly points out) equivalent to a class except for the default access (and inheritance).

However, USUALLY one uses the keyword 'class' if one is doing inheritance, virtual functions etc, and USUALLY uses 'struct' for C-style structs (aggregates or PODs)

Regarding memory allocation, if a class (or struct) uses virtual functions, then an extra pointers is (transparently) prepended to the memory block for the class (in most implementations).  The pointer points to a lookup table (vtable) that has the actually addresses of each of the virtual functions for the class.  This lookup table is shared for all objects of a given class and each class (that has virtual functions) has its own vtable.

That is the only difference between POD's (or aggregates or C-style structs) and classes as far as storage is required.

As far as allocating, you cannot (or at least should not) allocate the memory for a class that has constructors or virtual functions (ie NOT a POD) using malloc/free.  This is because when you do this the memory is allocated, but the constructors are not called and so the members are not correctly initialized and the vtable poitner (if any) is not set up.  This can result in fatal and/or hard-to-find errors.

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Do you not think I (or nietod or alexo) deserved some points here.  We put a lot of work into this question. Especially after you said "Dear ronslow: Your answer is very excellent.I accepted it gradly."

Perhaps you did not understand the grading system?

0
 

Author Comment

by:youngest
Comment Utility
Dear Ronslow:
 sorry!
 YES,I want to grade you,but I can't  find your name,there
is a sole name.
so,Can you tell me how to do.
Thank you for your help again!

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
You have to reject the current proposed answer (ie. reopen the question).  Then I can place an answer.  Noone else is allowed to propose ansers when one is still pending a grade (dreamPeace has a proposed answer awaiting a grade).  All we can do is post comments.  And you can only grade a proposed answer.

What you as a customer should do is evaluate and proposed answer as soon as possible (this may involve getting more info from the expert who propoed it, or from othe epxerts).  If you feel it is a suitable solution, and no other expert has suggested something better as a comment, then accept the propsed answer.  If a better solution is suggested as a comment (or the solution proposed is just not good enough) then reject the proposed answer.  This re-openes the question so other experts can propose answers.

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Ooop .. forgot that you'd already graded this.

Linda (from EE) has now given me some point for this question on your behalf.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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