+ 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; }

12th Mar 2017, 11:14 AM
keerthiga
keerthiga - avatar
5 Antworten
+ 2
s3 constructor has to display the constant value of(5 B 6).what can i declare for that parameterized value??
14th Mar 2017, 1:15 PM
keerthiga
keerthiga - avatar
+ 1
This code has some compilation error.
12th Mar 2017, 12:51 PM
keerthiga
keerthiga - avatar
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; }
12th Mar 2017, 12:32 PM
kibbekPL
kibbekPL - avatar
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;
12th Mar 2017, 1:13 PM
kibbekPL
kibbekPL - avatar
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 :)
14th Mar 2017, 8:03 PM
kibbekPL
kibbekPL - avatar