+ 2
How to declare a constructor for the following code?
#include <iostream> using namespace std; class Sample { public: int data_, graph_; char data_or_graph_; Sample(-----------): ----------------{ cout << data_ << " " << data_or_graph_<< " " << graph_ <<endl; } }; int main() { int x; char y; cin>>x >> y ; Sample s1(x,y), s2(--x, ++y), s3; return 0; }
5 ответов
+ 2
s3 constructor has to display the constant value of(5 B 6).what can i declare for that parameterized value??
+ 1
This code has some compilation error.
0
#include <iostream>
using namespace std;
class Sample {
public:
int data_, graph_;
char data_or_graph_;
Sample() {}; // first constructor
Sample(int x, char y ) : data_(x), data_or_graph_(y){
cout << data_ << " " << data_or_graph_ << " " << graph_ << endl;
} //second constructor
};
int main() {
int x; char y;
cin >> x >> y;
Sample s1(x, y), s2(--x, ++y), s3;
return 0;
}
0
It works 100% I checked it in Visual Studio and Sololearn compilator
Maybe change:
cout << data_ << " " << data_or_graph_ << " " << graph_ << endl;
to:
cout << data_ << " " << data_or_graph_ << endl;
because the graph_ is empty;
0
#include <iostream>
using namespace std;
class Sample {
public:
int data_;
int a, b;
char t;
char data_or_graph_;
Sample() {
cout << "Hi I'am s3 constructor"<<endl;
}; // first constructor EMPTY
Sample(int x, char y) : data_(x), data_or_graph_(y) {
cout << "Hi I'am s1 or s2 constructor" << endl;
cout << data_ << " " << data_or_graph_ << endl;
} //second constructor
Sample(int x, char y,int z) : a(x), t(y), b(y) {
cout << "Hi I'am s4 constructor" << endl;
cout << a << " " << b << " " << t << endl;
} //third constructor
};
int main() {
int x; char y;
cin >> x >> y;
Sample s1(x, y), s2(--x, ++y), s3, s4(5,'B',6);
return 0;
}
You can overload constructors
If you want learn c++ quickly and good you should buy thinking in c++ Bruce Eckel :)