Strings and Constructors.

Hey people, I started learning c++ and i have a few Questions:

1* Can anyone tell me what a constructor is in a simple way and easy to memorize.

2*And how can i acess variables in a private class WITHOUT using the complicated "string getName" and stuff like that.If There isnt can anyone explain how that works and if its really necessery to use and EASY way to memorize it??


For anyone who awnsers THANK YOU, for taking your time to help someone out.


 
  Put (No code required, only questions)
1
2
3
4
5
6
7
8
9
10
11
class Line
{
   public:
      double getLength( void );
      Line();  // This is the  a default constructor
Line(x){length = x;}; // this constructor gives length value of x
   private:
      double length;
}obj1, obj2(4); //length of obj1 has no value, but length of obj2 = 4




so basically class constructor is a member function of a class that is executed whenever we create new objects of that class.

A constructor will have exact same name as the class and it doesn't have any return type.

to access variable in this case you would say obj2.length but I am not sure if that is what you're asking about
Like ebucna said for constructors. Additionally, if you do not declare any yourself, a default constructor will automatically be created for you. You probably know this, but since ebucna didn't mention it, you don't explicitly call the constructor. It is done automatically on object creation, and is only ever called once.
A default destructor will also be created for you if you do not declare one. http://en.cppreference.com/w/cpp/language/destructor

As for part 2, if a variable (or method) is private, only the class's own members have access to those members (as well as "friends"). The only way to get access from outside the class is to use some method that exposes that value (like a getter method).
http://www.cplusplus.com/doc/tutorial/classes/

Maybe you can think of it like a private club. You can bring your friends there if you are a member of that club, and they can come into the club with you, but everyone else can only just look in from the outside through whatever windows you have there. If you want to show those outsiders what is inside that club, you can expose it through those windows but they can't go in themselves to see it. Those getter/setter methods would be your path of communication to ask for things/tell them to do things since you can actually manipulate the club yourself from the outside.
It can get more complicated than that, but maybe that'll help you understand a bit more about it to start with.

If you make a variable public, however, you can just access it by doing myclass.variable_name.

Protected is like private, but members of derived classes also have access. With private members they do not.
Last edited on
I still cant get it, maybe im dumb.

I Guess ill study more. Thanks anyway. Ill leave this part blank in the test.
Is there a particular part of the concept you're having trouble with? If you let us know, we can help you better.
Yeah, I don't understand why do I need to write all of that and memorize that just for a string of text is that really necessary?


And the "getName" , "setName" what are they for?
And why do I need to associate a string with an variable?

If it is in the private class is suposed to be private?



And last question , when i create classes in different files and i write "#include (file name).h" it says that it's not defined on scope , why?



Sorry for asking TOO many questions , Im learning with tutorials and i don't have a teacher to ask questions to (I do but i don't like how he explains).


Thanks alot.
All in all, a constructor is just a special method (member function) that is called when you create an object of that class. Constructors can take arguments, but it isn't necessary. The default constructor will take no parameters.

get... and set... are getter/setter methods. If your variables are private, the only way to see the values/change them from outside the class is to use methods. Think of them like the middle man. You tell them to change something, and they change it for you. You ask them what something looks like and they'll look at it and tell you what it looks like. You just can't change it/look at it directly yourself.

You can use get... to get the value of a variable. You can set set... to change the value of that variable (but you'll pass a string/int/whatever as a parameter). You can really name those methods anything you want, but prefixing get/set is the usual way of indicating that they are getter/setter methods.

1
2
3
4
5
6
7
8
9
10
11
12
class myClass
{
public:
  myClass();
  ~myClass();

  void setName(std::string newName);
  std::string getName();

private:
  std::string name;
};


1
2
3
4
5
6
7
8
9
void myClass::setName(std::string newName)
{
  name = newName;
}

std::string myClass::getName()
{
  return name;
}

And then somewhere else
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  myClass myClassInstance; //When you create an object, the constructor is called

  //std::cout << myClassInstance.name //Can't do this, since 'name' is private.
  std::cout << myClassInstance.getName() << std::endl; //Now we can see what the value of 'name' is

  //myClassInstance.name = "New Name"; //We can't do this, since 'name' is private. We don't have access here
  myClassInstance.setName("New Name"); //We CAN do this. Pass a string to the setter method, and it will change the value of 'name' for us.

  std::cout << myClassInstance.getName() << std::endl;
}


You associate the string with a variable so that you have something to store it in and refer to it.
If you don't store it in a variable, you can't really do much with it.

Is the header file in the same folder as the file you're trying to include it in? If not, you'll need to provide the path to that file as well.

As an comment on code given above:
1
2
3
      double getLength( void );
      Line();  // This is the  a default constructor
Line(x){length = x;};
could use some changes;

double getLength( void ); void does not need to be in there. It can just be double getLength();. void can be there, but it generally considered a c-style idiom.

Line(x){ length = x;};
Should be
Line(double x){ length = x;}
The type of the parameter must be given.

ebucna wrote:
to access variable in this case you would say obj2.length

You wouldn't be able to access it in that way because length was declared as private.
Yeah , thank you!

Too much to process for me cause im just a noob in this.

Im learning from the "newboston" and I don't know what "sts: :" means but i don't want to take more of your time and il'l google it-

If you don't mind im copying what you did and paste so i can see it whenever i have problems.

is there any site that offers like a teacher online that helps you and teaches you for free or low cost?


Thanks alot!!
Multi6556

I do not know of any online teachers, but i have found "http://www.learncpp.com/" to be a very easy to understand tutorials. I have learned things there about classes that several books and years did not work. The pages here are also very useful.

Hope that helps,

Andy
Last edited on
Handy Andy


Thanks alot ^^
Topic archived. No new replies allowed.