C++

Simple Class Example in C++

Source Code

A simple class example called Point, with all necessary functions

Just Copy Paste in DevC++ 5.11 and Run for Practice

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# include<iostream>
# include<conio.h>
# include<math.h>

using namespace std;

class point
{
	int x,y,z;
public:
	point()
	{
		x=y=z=0;
	}
	point(int i,int j,int k)
	{
		x=i;
		y=j;
		z=k;
	}
	point(point &a)
	{
		x=a.x;
		y=a.y;
		z=a.z;
	}

	negate()
	{
		x=-x;
		y=-y;
		z=-z;
	}
	void print()
	{
		cout<<"("<<x<<","<<y<<","<<z<<")";
	}
	int norm()
	{
		return(sqrt(x*x+y*y+z*z));
	}
};
main()
{
point p(2,3,4),p1(p);

cout<<"The point has the coordinates \n";
p.print();
cout<<"The point coordinates after negation \n";
p.negate();
p.print();
cout<<"\nNormal Distance of the point from (0,0,0) is "<<p.norm();
cout<<"\nThe coordinates of the point p1 after copy constructor is ";
p1.print();
getch();
}

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Close
Back to top button
Close
Close

Adblock Detected

Please consider supporting us by disabling your ad blocker