LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2005, 12:14 AM   #16
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled

To initialise member data when you create an object?
 
Old 08-27-2005, 01:37 AM   #17
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
You need a constructor to actually create the object. When you create an object, the compiler will set aside memory, etc.

Using mutator functions, you are simply changing variables that already exist. A mutator function can not be used to create an object.

--Ian
 
Old 08-27-2005, 07:08 AM   #18
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by IBall
You need a constructor to actually create the object. When you create an object, the compiler will set aside memory, etc.

--Ian
This is what I mean... I have created an object named "Frisky", without using the constructor. I created "Frisky" by just adding
Code:
Cat Frisky;
. So why would I use the constructor, I still don't understand. Sorry I'm still a
 
Old 08-27-2005, 09:03 AM   #19
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally posted by InvisibleSniper
This is what I mean... I have created an object named "Frisky", without using the constructor. I created "Frisky" by just adding
Code:
Cat Frisky;
. So why would I use the constructor, I still don't understand. Sorry I'm still a
You did use a constructor, the default constructor that takes no arguments.
 
Old 08-27-2005, 01:29 PM   #20
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
  • And if I was to write
    Code:
    Cat Frisky(5);
    the (5) part would be initializing a member variable, is that right?
  • And also what is the most common use for constructors, like what part of a program?
  • I also know that you can overload constructors... so is it a good idea to initialize all of the member variables using overloaded constructors?
Also am I right here in saying that... whenever you declare an object for example
Code:
Cat Frisky;
and the constructor takes a parameter for example
Code:
class Cat{
Cat(int age);
~Cat();

};
Then you have to include the integer parameter when you declare the object like
Code:
Cat Frisky(5);
is this right?

Last edited by InvisibleSniper; 08-27-2005 at 01:44 PM.
 
Old 08-27-2005, 01:52 PM   #21
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Quote:
Originally posted by InvisibleSniper
  • And if I was to write
    Code:
    Cat Frisky(5);
    the (5) part would be initializing a member variable, is that right?
  • And also what is the most common use for constructors, like what part of a program?



  • I also know that you can overload constructors... so is it a good idea to initialize all of the member variables using overloaded constructors?
You are creating a new object of type 'Cat', tagged with the name 'Frisky'. You are passing the literal integer argument '5' to the Cat constructor which recieves an integer argument. You are probably initializing a member variable, but technically you dont have to. Also there is a difference between initialization and assignment. Don't worry too much about this now, but keep that fact in your head for a while.

As for overloading constructors... the general rule is to write constructors that need to be written. Heres a fairly simple class, with 2 constructors. I'm going to make the first constructor the default constructor by giving it default arguments. Normally default constructors take no arguments. Using default arguments allows you to create an object without providing arguments, because the defaults will be used. The second constructor is a copy constructor - it recieves a const reference to an object that is the same type as the class. Since we are writing mroe than one constructor, this is an example of overloading constructors. It is pretty normal, most useful classes will have at least 2-6 constructors implemented.

Code:
class Cat
{
public:
  Cat(const string = "default cat");  // default ctor
  Cat(const Cat &);  // copy ctor

private:
  string name;
};
I won't bother writing the constructors because I dont see much point. I remember you have seen default arguments before so I think you will understand most of this. You might have never seen copy constructors before, but dont worry too much if you havent. Its just good to know they are there, and you can indeed pass objects of the same type to constructors.

I hope some of this helped.
 
Old 09-03-2005, 05:45 AM   #22
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
If I was to work in a buisiness later on in my life programming in C++, what would I use classes for?
 
Old 09-03-2005, 12:37 PM   #23
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Thumbs up

Quote:
Originally posted by InvisibleSniper
If I was to work in a buisiness later on in my life programming in C++, what would I use classes for?
Just about everything. Keep learning them if you're intested in C++ (which it seems like you are). You generally don't use functions as much in C++, you use classes with class methods. Once you learn a whole lot more about classes including inheritance and polymorphism you should pick up a book on object design. "Object Design - Roles, Responsibilities and Collaborations" by Rebecca Wirfs-Brock and Alan McKean is a good example of such a book. Using classes just for the sake of using them is generally pointless. They don't get very useful until you have many classes in a large program, with objects interacting back and forth.
 
Old 09-03-2005, 01:07 PM   #24
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Can classes have other files as member data and/or pitcures as member data? Or are the basic types of C++ the only thing they have?
 
Old 09-03-2005, 01:10 PM   #25
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Classes can have objects of other classes as member data, so if you had a File class, you could use that to represent files in your other classes.
 
Old 09-03-2005, 08:36 PM   #26
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Quote:
Originally posted by InvisibleSniper
Sorry for double posting but can someone tell me what the big advantage is of using a constructor other then using a accessor function?
A constructor will be called regardless, so it is to your advantage to use this. If the constructor is called and you don't initialze your members properly, you need to use an accessor to do it. Basically 2 function calls vs. 1 call (to the constructor).
 
Old 09-04-2005, 11:17 AM   #27
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
Constructors are always called, when you create an object. Thus you are
sure that your object becomes a valid state, when you create it. For instance, your object consists of several other objects, not only simple
instance variables. Actually the construction of objects is an important task in object-oriented programming and there are a few design-patters which work
a bit different than constuctors.

But you may have a look at design-patterns.
 
Old 09-05-2005, 01:22 PM   #28
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Can you give me one more N00Bish example of a constructor getting called please.

Also I was reading about Classes and came accross this statement,
Quote:
"Keep in mind that neither constructors nor destructors return arguments! This means you do not want to (and cannot) return a value in them. "
I kind of don't understand that above statement can someone give me an example of what he means, or does he just mean that you can not return a value like you would with a function?
 
Old 09-05-2005, 03:23 PM   #29
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Code:
~RandomDestructor()
{
  // free resources
  return 0;  // illegal
}
 
Old 09-05-2005, 07:15 PM   #30
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
I wrote me another noobish for practice, and there is one thing I can't understand. Everyone says I should make the variables private so I did. Now I don't know how to use my variables without using it in a function. Here is my code, it is commented well enough... I hope :|.

The Header File
Code:
#include <iostream>

using namespace std;

class Microwave{

public:
Microwave();
~Microwave();
void foodReady();//Beeps when food is ready
void cookChicken();
void cookPie();
private:
float cookPieTime = 3.00;
float cookChickenTime = 20.00;
//These are the private variables that you should not need to know about since all you have to do is press
//button to start the preprogrammed cooking times.
};
And the source file:

Code:
//What I am trying to do here is represent a microwave's preprogrammed
//cooking times in object orientated form.

#include "Microwave Class.hpp"



Microwave::Microwave()
{
}
Microwave::~Microwave()
{
}
void Microwave::foodReady()
{
cout<<"Beep, beep, beeeeeeep!!!!!\n";
}


void Microwave::cookChicken()
{
cout<<"Your chicken will be ready in "<< <<" minutes.\n"//I want to put the preprogrammed time to cook a chicken here
//but the variable, cookChickenTime is private, what do I do?

}

void Microwave::cookPie()
{
cout<<"Your pie will be ready in "<< <<" minutes.\n" //As above I can not access the preprogrammed cookPieTime
}
int main()

{
Microwave panasonicMicroWave;
int foodSelection = 0;
cout<<"1.Pie";
cout<<"2.Chicken";
cout<<"Please enter a selection of food you would like cooked and press enter: ";
cin>>foodSelection;//Put the selection in a variable for later processing.

switch(foodSelection)
{
case 1:
panasonicMicroWave.cookPie();
panasonicMicroWave.foodReady();
break;
case 2:
panasonicMicroWave.cookChicken();
panasonicMicroWave.foodReady();
break;
default:
cout<<"Error, bad input".
};
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP in PHP patpawlowski Programming 5 11-20-2005 04:37 PM
What is true OOP? tumana Programming 4 09-13-2004 06:51 AM
Why use OOP when OBP will do? unholy Programming 2 08-27-2004 04:56 PM
When to use OOP KptnKrill Programming 10 08-24-2003 01:15 PM
newbie oop Isitme Programming 3 10-10-2001 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration