Cant seem to understand this basic class problem, can someone explain it to me?

Hello fellow programmers I am currently studying classes and objects and i cant seem to understand the following program. Can someone please explain to me how this program works? :D


#include <iostream>
using namespace std;

class A {
public:
float v;
A() : v(1.0) {}
A(A &a) : v(2.0) {}
A(float f) : v(3.0) {}
float get() {
return A::v;
}
};

int main() {
A a, b(1), c(b);
cout << a.v << b.v << c.v;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class A {
public:
float v; // Simple float variable
A() : v(1.0) {} // A default constr uctor that initializes v to 1.0, It's like writing v = 1.0;
A(A &a) : v(2.0) {} // A constructor that takes an Instance of A, and initializes v to 2.0
A(float f) : v(3.0) {} // Takes a float and initializes v to 3.0f
float get() {
return A::v; // Im not sure why it's A::v, you can just do return v;
}
};

int main() {
A a, b(1), c(b); // Creates 3 instances of A, one for each constructor explaned above
cout << a.v << b.v << c.v; // For some reason prints out v directly instead of using the get function.
return 0;
}


Overall it's a pretty poorly made program.

Edit: And dont forget to always use code tags for all your code like I just did, you can clearly see that it is much easier to read code - http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Let's start with the class. It's written in a compact style which is not helpful. If someone wrote this I'd zap them with a taser and tell them to do it again properly.

1
2
3
4
5
6
7
8
9
10
class A {
public:
float v;
A() : v(1.0) {}
A(A &a) : v(2.0) {}
A(float f) : v(3.0) {}
float get() {
return A::v;
}
};


Line 1: This is a new kind of object that can be created. These objects are known b the type identified "A".
Line 2: Everything that follows is accessible (so functions can be called, variables can be read/written) by things that are not of type "A"
Line 3: This class contains a float variable, named v
Line 4: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This function has an initialiser list v(1.0) so when this constructor is called, v is set to 1.0
Line 5: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This constructor accepts a reference to an object of type "A". This is the copy constructor, although it should really use a const reference. This constructor will be called if we make an object of type A by copying an existing object of type A. http://www.cplusplus.com/forum/articles/18749/
Line 6: This is a constructor function, all written on one line. You can tell it's a constructor function because it has no return type and the function has the same name ("A") as the class. This constructor accepts a float parameter (although it doesn't actually do anything with that float parameter). This constructor also has an initialiser list.
Line 7-9: This is a class function. It returns the class variable v.


 
A a, b(1), c(b);
This line creates three objects of type A. This first one is created using the constructor on line 4. This second one is created using the constructor on line 5. This third one is created using the constructor on line 6.
Last edited on
Hi! Sure, no problem;

It might be a good idea to enclose your code in "code" tags to make it more readable as well ;)

1
2
3
4
5
6
7
8
9
10
11
class A
{
  public:
    float v; //This is a data member - it holds data
    A() : v(1.0) { } //In short, A() is a constructor -a method that gives 'v' 
                           //the value 1.0
    float get()
    {
      return A::v; //This is a method that returns the value of 'v'.
    }
};


Suffice it to say, the above code block is a class declaration; with it, you are describing an OBJECT - a block of memory - and stating what information it can store (in this case, a float value - 'v').

You are also stating what operations can be performed on the object with the method functions, such as get().

In main(), all you're doing is declaring three objects, three variables of class 'A' - a, b, and c. You are then outputting the value stored in data member 'v' of all three objects.

Think of an object as a box, and the data members as "compartments" within that box. Each "compartment" holds a separate value. The class are the instructions from which the compiler will "construct" that box and its compartments :)

Really, that's the shortened version. I hope it helps. I know of some youtube vids that really explain objects well, I'll post it if I find them. :)

You'll also note that I skipped out on a few methods, it's too complex for me to discuss in one single post. But I think you'll get the idea real soon.

Joe - Concord Spark Tutor
sparkprogrammer@gmail.com
https://www.facebook.com/Concord-Spark-Tutoring-415848731922060/?ref=hl
Last edited on
Thanks guys!! :D
Topic archived. No new replies allowed.