Learning parameters and classes. Guide me to do this question.

For this question:
Write a class with three integer attributes specified as public. Using a parameterized constructor assign values to these attributes from the main function.

I did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
  
class myClass {
	public:
		int a,b,c;
		myClass(int A, int B, int C){
			a = A;
			b = B;
			c = C;
		}
};

int main() {
	myClass s1(5,6,2);
	cout<<s1.a<<endl<<s1.b<<endl<<s1.c;
}


Is this correct?


The next question says:
Rewrite the program in Q3 specifying the attributes as private. Change the code accordingly to get the same output.


So. Im not asking for direct answers. I just need some guidance. I donot understand when, where and why parameters or constructors are used. So, I'd like to learn that first. After that, I want to figure out how to do the second question.
Last edited on
In tutorial: http://www.cplusplus.com/doc/tutorial/classes/
constructor is automatically called whenever a new object of this class is created, allowing the class to initialize member variables

A constructor is called every time you declare a class variable.



There is a difference between:
1
2
3
4
int a = A; // initialization
// and
int a;
a = A; // assignment 

You do assignments in your constructor.

A class constructor can have a member initializer list. See https://en.cppreference.com/w/cpp/language/constructor
You constructor with member initializer list:
1
2
3
4
myClass( int A, int B, int C )
 : a{A}, b{B}, c{C}
{
}

Note that the body of the constructor is empty.

It is better to initialize new object with value than assign the value later (if possible).
In some cases object cannot be assigned to and must be initialized.
Its still a bit unclear. But, is constructor used so that the compiler wont give the error "variable a is not declared"? Something like that?

Also, does my piece of code have a constructor in it? Im not sure. I also dont which part you are referring to as "the body of the constructor"..
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

class myClass {
public:
	int a {}, b {}, c {};
	myClass(int A, int B, int C) : a(A), b(B), c(C) {}
};

int main() {
	myClass s1(5, 6, 2);
	cout << s1.a << endl << s1.b << endl << s1.c;
}


This is the constructor:

 
myClass(int A, int B, int C) : a(A), b(B), c(C) {}


It is called every time the class is instantiated with 3 int parameters.

The part between the : and { are the class variables initialisation. The part between the { and } is the body of the constructor (same as a body of a function}. This sets a to the value of A, b to B, c to C. The body has no statements.
Last edited on
Line 11 above declares a variable:
myClass s1(5, 6, 2);

There is the "normal" declaration myClass s1 ;, name "s1", type "myClass".
There are also values for initialization (5, 6, 2)
Therefore, the constructor myClass(int, int, int) is automatically called during that declaration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

class myClass {
public:
	int a {}, b {}, c {};
	myClass(int A, int B, int C) : a(A), b(B), c(C) {}
};

void print( myClass x ) {
  cout << x.a << endl << x.b << endl << x.c;
}

int main() {
  myClass s1(5, 6, 2);
  print( s1 );
}

This version has two calls to constructors of myClass.
On line 15, when s1 is declared
and
on line 16. The function call takes myClass object by value parameter.

Effectively, that function call declares
myClass x( s1 );

That constructor is not the myClass(int, int, int). Compiler has automatically created another constructor for myClass:
myClass( const myClass& )
That is known as copy constructor.
Is this the same thing as

1
2
3
4
5
myClass(int A, int B, int C){
			a = A;
			b = B;
			c = C;
		}



this one

1
2
int a {}, b {}, c {};
	myClass(int A, int B, int C) : a(A), b(B), c(C) {}




And what is the difference between these two

int a {}, b {}, c {};

int a, int b, int c;
1) nearly. In the first a, b, c are defined and then initialised - as 2 steps. In the second a, b, c are defined and initialised as 1 step. This is similar to:

1
2
int a;
a = 1;


and

 
int a {1};


2) The first default initialises the variables (to 0 for int). The second doesn't initialise. Their values are unknown until a value is assigned to them.
Initialization and assigment of basic types, like int, is not much different, but there are types that cannot be assigned at all.
For example, const int:
1
2
3
4
5
6
7
8
9
10
11
class myClass {
public:
  const int a;

  myClass( int A )
  : a(A) // ok
  {
    a = A; // error: cannot assign to const
  }

};


And what is the difference between these two

int a {}, b {}, c {};

int a, int b, int c;

The second is a syntax error. You could write:
int a, b, c;
or
int a; int b; int c;
While these look "compact", they do not really give any advantage. Quite opposite, they can confuse. It is better to have them on separate statements and lines:
1
2
3
int a;
int b;
int c;

These three variable declarations do not have initializers. There are (unfortunately) more than one syntax for initialization:
1
2
3
4
int a; // uninitialized. Unknown value
int b {}; // initialized. Value == 0
int c {7}; // initialized. Value == 7
int d = 42; // initialized. Value == 42 


C++11 allows initializers in class definition:
1
2
3
4
5
6
7
8
9
10
11
12
class myClass {
public:
  int a {2};
  int b {3};
  int c {4};

  myClass( int A, int C )
  : a(A),
    // b will get value 3
    c(C)
  {}
};
Topic archived. No new replies allowed.