C++ examples for Class:Constructor
Initialize objects using default copy constructor
#include <iostream> using namespace std; class Measure//from w w w. j av a 2 s .c om { private: int feet; float inches; public: Measure() : feet(0), inches(0.0){} //Note: no one-arg constructor Measure(int ft, float in) : feet(ft), inches(in){} void getdist() //get length from user { cout << "\nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; } void showdist() { cout << feet << "\'-" << inches << '\"'; } }; int main() { Measure dist1(11, 6.25); Measure dist2(dist1); Measure dist3 = dist1; cout << "\ndist1 = "; dist1.showdist(); cout << "\ndist2 = "; dist2.showdist(); cout << "\ndist3 = "; dist3.showdist(); cout << endl; return 0; }