Re: few questions concerning classes
"alternativa" <alternativa.4@wp.pl> wrote in message
news:1146315379.192057.322930@g10g2000cwb.googlegroups.com...
Hello,
I have a few questions concerning classes.
1)
Why some people use default constructos, i.e constructors with no
parameters? To me it doesn't make any sense, is there something I
should know? For example, I'd declare a class in a following way:
class Sample {
int number;
string title;
public:
Sample (int n, string t);
~Sample ();
}
What is the sense in adding also
Sample() {}; in public part of a class?
Default ctors are sometimes required (ie: primitive containers). They also
help you define what a default object looks like. That can help you in so
many ways. By the way, your Sample class above has no def ctor, the compiler
will not generate one for you.
#include <string>
class Sample
{
int number;
std::string title;
public:
Sample() : number(0), title("unknown") { } // def ctor
Sample(int n, std::string s) : number(n), title(s) { }
~Sample () { }
};
int main()
{
Sample samples[10]; // all 10 elements are inited with 0 and "unknown".
}
2)
If I have a few classes containing lists and one main class containing
head pointers to these lists, should I write destructors for all the
classes separately, or is it enough to write a destructor for main
class only? For example:
class Numbers {
int num;
Numbers *numNext;
public:
Numbers () { /*constructor body*/ };
~Numbers () { /*empty*/ };
};
class Titles {
int tit;
Titles *titNext;
public:
Titles () { /*constructor body*/ };
~Titles () { /*empty*/ };
};
class Together {
*numHead;
*titHead;
public:
Together (){ /*constructor body*/ };
~Together(){ /*destructor body*/ };
};
Is this ok?
Not really. Titles and Numbers look like the components of the previous
Sample class. Why not just build a list of Samples instead of banging your
head with pointers? Goal #1 is to never, ever use pointers unless absolutely
neccessary.
std::list<Sample> samples;
samples.push_back(0, "title zero");
samples.push_back(1, "title one");
3) inheritance problem
I've got the following base class:
class Person {
public:
string name;
int phone;
Person(string n, int p);
~Person();
}
and inherited class:
class Student {
int mark;
public:
Student(string n, int p, int m) ;
~Student();
}
I know I shoudl write the base class in a way that data are in a
private part,but then it doesn't work. How to deal with it?
with init lists...
The data should be encapsulated otherwise you can't guarentee nor control
the state of the objects the classes create. Don't forget your semicolons
when you declare your classes. Inheritance is propagated by you, not the
compiler. You have to tell the compiler what the relationship between
classes are.
#include <iostream>
#include <ostream>
#include <vector>
#include <string>
class Person
{
int id;
std::string name;
public:
Person(int n, std::string s) : id(n), name(s) { }
~Person() { }
/* member functions */
int getID() const { return id; }
std::string getName() const { return name; }
};
class Student : public Person // ie: a Student is_a Person
{
double mark;
public:
Student(int n, std::string s, double d) : Person(n, s), mark(d) { }
~Student() { }
/* member functions */
double getMark() const { return mark; }
};
int main()
{
std::vector<Student> students;
students.push_back(0, "Annie", 75.1);
students.push_back(1, "Bob", 65.5);
students.push_back(2, "Cathy", 70.9);
for (int i = 0; i < students.size(); ++i)
{
std::cout << "id: " << students[i].getID(); // a Student is_a
Person
std::cout << " name: " << students[i].getName();
std::cout << "\tmark: " << students[i].getMark();
std::cout << std::endl;
}
}
Set breakpoints and step into the ctors and into the member functions to get
the picture.
If std::vectors are baffling you, ask someone to give you an example with a
dumb array. Unfortunately, that would mean def ctors + setter functions.