473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

few questions concerning classes

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?

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?

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?

Apr 29 '06 #1
7 1737
"alternativa" writes:
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?


Consider this.

int a = 1024;
int b = 2048;
int c = 4096;
c = a + b;

This gives the impression that knowledge exists and it is simply fraud.
There is no meaning at all to the value given when c is defined. It
misleads people.

That alone should be sufficient reason. If not, you can not have an array
of objects unless there is a default constructor.
Note also, that as soon as you declare a constructor the default constructor
goes away, you have to explicitly bring it back, as above.

<snip>

I don't like multiple part questions. It's hard enough to keep one single
thread on track.

Apr 29 '06 #2
"alternativa" <al***********@wp.pl> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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?
In some contexts, it is not possible to call anything other than a default
constructor, e.g.,

Sample * array = new Sample[10];

It is also sometimes the case that

a) a class has no data members, or
b) it is not known at the time of declaration what those data members need
to be initialized to.


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?
You haven't given the type of numHead and titHead. I presume you mean:

Numbers *numHead;
Titles *titHead;

Whether it is OK depends on the nature of the destructor. In Together's
destructor, you could iterate along each list and delete each link. It is
much simpler, however, to write:

~Numbers () { delete numNext; }
~Titles () { delete titNext; }
~Together(){ delete numHead; delete titHead; }

Simpler still is to use std::list.
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?


"Doesn't work" isn't very helpful information. How doesn't it work? What are
you trying to do?
--
John Carson
Apr 29 '06 #3
> "Doesn't work" isn't very helpful information. How doesn't it work? What are
you trying to do?


When I try to compile the program which also contains some other
methods of derived class (for example method printing the data of
derived class - of course of a base class also), then it is not
possible and the comment is 'class string Person::name' is private'.
When I put 'protected' instead of 'private' in basic class Person, then
it's okay, but I have some doubts whether it's the best solution.
regards,
a.

Apr 29 '06 #4
"alternativa" <al***********@wp.pl> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com
"Doesn't work" isn't very helpful information. How doesn't it work?
What are you trying to do?


When I try to compile the program which also contains some other
methods of derived class (for example method printing the data of
derived class - of course of a base class also), then it is not
possible and the comment is 'class string Person::name' is private'.
When I put 'protected' instead of 'private' in basic class Person,
then it's okay, but I have some doubts whether it's the best solution.
regards,
a.

You can get around in in various ways. For example, the base class could
have a public or protected PrintData function. The PrintData function in the
derived class would first call the PrintData function from the base class
before outputting its own data.

For more flexibility, the base class can have (public or protected) accessor
functions that return either copies of the data (so modifying the returned
data doesn't affect the data stored in the base class instance) or const
references to the data (so the returned data can't be modified). The
functions in the derived class that need read-only access to the base class
data can access it through these functions.

If the derived class actually needs to modify the data in the base class,
then the base class can provide functions for modifying the data. These
functions can do range checks or whatever else may be deemed necessary to
provide for "safe" modifications.

--
John Carson
Apr 30 '06 #5

"alternativa" <al***********@wp.pl> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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.
Apr 30 '06 #6
Salt_Peter wrote:
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);


This is supposed to compile? You probably meant:

students.push_back(Student(0, "Annie", 75.1));
students.push_back(Student(1, "Bob", 65.5));
students.push_back(Student(2, "Cathy", 70.9));

Apr 30 '06 #7
Markus Schoder wrote:
Salt_Peter wrote:
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);


This is supposed to compile? You probably meant:

students.push_back(Student(0, "Annie", 75.1));
students.push_back(Student(1, "Bob", 65.5));
students.push_back(Student(2, "Cathy", 70.9));


Yep, thanks
Apr 30 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Varn | last post by:
I have a couple questions/comments concerning dot net and the future of dot net development. If someone could address these, please do so. First of all, with the advent of .net and the use of...
2
by: frederik12 | last post by:
Hi all, Today I downloaded and installed VS 2005 C# Express Edition (.NET framework 2.0). I'm very pleased about this new product. In my current application I'm using the WebBrowser control in...
0
by: Joe Abou Jaoude | last post by:
Hi all, I have developped a asp.net application, and now I have to install it on the client's computer. I have 2 questions concerning the installation: 1-Under the Web application I have an...
24
by: CJ Taylor | last post by:
Cmon.. I help out. =)
4
by: Yoram Biberman | last post by:
I have a few questions concerning concurrency control. I shall thank whoever can help me. Question #1 ========= Assume the following (concurrent) schedule, in which both transactions run in a...
1
by: Markus Stehle | last post by:
Hi all! We are planning to provide .net web services to our partners. As I am new to web services technology, I have some questions concerning data exchange and interop. As most of our...
8
by: NH | last post by:
Hi, I'm looking for some opinions and advice on designing ASP.Net apps. Let me explain my approach to how I currently design and hopefully you can give me some advice. I create systems for a...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
22
by: gustum | last post by:
Im graduating in the coming december. Anyone pls guide me where i can get c++ interview questions. I shall be very thankful to you if you provide me good link so any stuff if you have regarding...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.