ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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).
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.
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?
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".
};
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.