Results 1 to 7 of 7

Thread: Basic Classes

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    47

    Basic Classes

    Hi, It's me again. I have 3 more questions concerning classes.
    1. I tried to make a simple program using class which would call functions to get information. Here's the code:
    #include <iostream.h>
    #include <stdlib.h>
    class employee
    {
    public:
    int age(int a); // defines employee;s age
    int years(int yrs); // defines years worked
    void speech(); // prints employee's speech
    };
    int employee::age(int a)
    {
    cout << "Enter a number.\n";
    cin >> a;
    return a; }
    int employee::years(int yrs)
    {
    cout << "Enter anothe number.\n";
    cin >> yrs;
    return yrs; }
    void employee::speech()
    {
    cout << "My name is Tom.\n";
    }
    int main()
    {

    employee tom;
    tom.speech();
    tom.age(int a);
    tom.years(int yrs);
    system("PAUSE");
    return 0;
    }

    2. I read over and over about constructors and destructors in classes but I still don't get the point. What and when is something erased from memory?
    3. Since I'm new to classes, what are they really for? Why can't I just use a function instead of grouping objects in a class? And what should the class return in a program?
    Thank You!

  2. #2
    Aragorn
    Guest
    I'm also kinda' newbie, but i'll try to help.
    a constructor packs all the things you have to do when you create a new object, in this case, it is nonsense to have in memory an employee without age and years worked, so when you create it, it will always have to ask for age and years. therefore you put those in the constructor, so that when you do

    employee tom;

    it automatically calls

    tom.speech();
    tom.age(int a);
    tom.years(int yrs);
    system("PAUSE");

    and you save 4 lines of code. Well, that's not all. You can use constructors to put the age and years beforehand

    employee(int a, int yrs) {
    age=a;
    years=yrs;
    }

    and there's more... probably

    the destructor has a similar function. its point is to set everything up before the object actually disappears. In your example it would have not much use, so i'll put my example;
    I'm working on a neural network. Each neuron has connections to other neurons (pointers), so when i erase neuron A from the network i want to make sure that all other neurons connected (pointing) to A have to be modified: that is, i have to erase pointers to A wherever they may be, so i use the destructor to do this job, and i don't have to bother anymore about lost pointers

    Classes are very useful in many ways, and i couldn't tell you all of them, but one good thing is that you use an interface (the public methods) to interact with the object, and the private methods and variables are intrinsic to the object (i would call them the engine of the object). So if you want to modify the way the object works (internally, for example for optimizing) you can change anything you want: as long as the interface remains the same, there will be no problem with the rest of the program.
    Also you can derive new objects from a previously defined one, to perform more specific tasks. this is called inheritance. you may also have heard about that... but let's keep it simple!

    quote: And what should the class return in a program?

    A class doesn't return anything. just as you can't call a class (it's not a function, it's more close to a typedef than to a function). Only its methods are functions, and they should return whatever you may need.

    hope it helps!

    ZZzzzzzzzzzz....

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by Aragorn

    I'm working on a neural network. Each neuron has connections to other neurons (pointers), so when i erase neuron A from the network i want to make sure that all other neurons connected (pointing) to A have to be modified: that is, i have to erase pointers to A wherever they may be, so i use the destructor to do this job, and i don't have to bother anymore about lost pointers
    just,want,to,make,sure,youre,not,undermining,object,orientation,heresorry,for,the,commas,my,space,is,on,vacation.
    Anyway,one,of,the,most,important,ideas,behind,OOP,is,encapsulation,and,protection.Your,objects,shoul d,contain,all,it's,data,unless,it's,part,of,an,interface.I,immediately,noticed,your,issue,of,deconne cting,pointers,to,it,and,it,shouldn't,be,done,by,searching,on,the,network,or,accessing,variables,out ,of,scope.
    You,should,solve,this,by,having,the,refered,object,register,it's,referators,either,by,direct,pointer s,or,by,the,usage,of,doublelinked-pointer-objects,which,disconnects,both,ends,at,terminations.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Kedaman, fix that bloody space bar...don't drag it here as well as on ICQ

    Take a look at the STL smart pointer classes like auto_ptr<>.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Aragorn
    Guest
    thank's for caring about my network, kedaman.
    If i got you right (which i'm not sure, for i'm not english-speaking, besides, you are much clearer with space bar i'm already doing it your way. Each cell records its in and out connections. Also connecting and deconnecting is done via interface. So, when i remove it, the destructor looks in the array (i call it port) of in and out connections and does the job "legally". I was going to post the code when i was further, but i will do it now so you can get my point, if you feel like.
    Note that it's still a draft! You shouldn't look at all the code, there are many provisional things, but the ADT cell is what matters us. I'd love a comment on it.

    The point are the plug_in(cell*), plug_out(cell*), un_plug_in(cell*) and un_plug_out(cell*) private methods, which are handled via the << and >> (public) operators, which i overloaded to connect & disconnect cells.

    BloodMachine34, you may also take a look. Maybe you find something interesting (at least something, i hope!).
    Anyway, if you just skip it i'll understand, i know getting into foreign code is hard and tedious.

    parksie: i'll do it.

    see you!
    Attached Files Attached Files

  6. #6
    Aragorn
    Guest
    anyway i like that thing about doublelinked pointers. i'll study it.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    HEhe parksie, i fixed my spacebar now, STL smart pointer.. hmm i'll have a look at that one, might be good for my apps unless it's not too big.

    The point are the plug_in(cell*), plug_out(cell*), un_plug_in(cell*) and un_plug_out(cell*) private methods, which are handled via the << and >> (public) operators, which i overloaded to connect & disconnect cells.
    Sounds very interesting, i'll have a look at your project

    Note, if you use virtual functions, your classes and derived classes will automatically include a v-table which makes access to them one step slower, most virtual inline functions outline and adds an overhead of 4 bytes per object. You might get concerned when you implement largescale instatiations of certain objects but most of the time virtual functions just makes your life easier Also the destructor must be virtual if you use virtual functions, otherways there will be memory leaks!
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width