Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C

Explicit Constructor in C++

Rate me:
Please Sign up or sign in to vote.
4.67/5 (120 votes)
18 Aug 2008CPOL2 min read 181.2K   270   45  
Explicit constructor in C++.
#include <iostream>
using std::cout;
using std::endl;

class complexNumbers {
  double real, img;
public:

  complexNumbers() : real(0), img(0) { }
  complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
  complexNumbers( double r, double i = 0.0) { real = r; img = i; }
  friend void display(complexNumbers cx);
};

void display(complexNumbers cx){
  cout<<"Real Part: "<<cx.real<<" Imag Part: "<<cx.img<<endl;
}

int main() {
  complexNumbers one(1);
  display(one);
  display(300);
  return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Rebaca Technologies
India India
int main(){
while(!isSleeping())
{
write_code();
}
return 0;
}

Comments and Discussions