// This tut contains Parameterized constructor and Default constructor.
#include<iostream>
using namespace std;
class Complex
{
int a, b;
public:
Complex(int, int); // Constructor declaration
void printNumber()
{
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
Complex ::Complex(int x, int y) // ----> This is a parameterized constructor as it takes 2 parameters
{
a = x;
b = y;
// cout<<"Hello world";
}
int main(){
// Implicit call
Complex a(4, 6);
a.printNumber();
// Explicit call
Complex b = Complex(5, 7);
b.printNumber();
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Point{
int x, y;
public:
Point(int a, int b){
x = a;
y = b;
}
void displayPoint(){
cout<<"The point is ("<<x<<", "<<y<<")"<<endl;
}
};
// Create a function (Hint: Make it a friend function) which takes 2 point objects and computes the distance between those 2 points
// Use these examples to check your code:
// Distance between (1, 1) and (1, 1) is 0
// Distance between (0, 1) and (0, 6) is 5
// Distance between (1, 0) and (70, 0) is 69
int main(){
Point p(1, 1);
p.displayPoint();
Point q(4, 6);
q.displayPoint();
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.