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()
{
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!
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
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.
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
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.
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.
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.