Table of objects

Hi! I have this kind of problem: My program should work like this:
1.Input the number of points that you want to create.
2.For each point write these:
name,x-position, y-position
3.Program should count the length from point to origin(0,0), it is counting in function Leng()
4.All parameters are saving in object abc
5.And when i want to create the table of object i get error: " no default constructor exist for Klasa"

Can someone can show me how to solve this problem and tell me what i have to do, to write on my console window all parameters of each point?


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
#include <iostream>
#include <string>
using namespace std;

class Klasa {
	string name;
	int    x,y;
	double rr,leng;
public:
	Klasa(string name, int x,int y) {
		this->name = name;
		this->x = x;
		this->y = y;
		this->leng = Leng();
	}
	double Leng(){
		double rr, sq;
		rr = x*x + y*y;
		sq = sqrt(rr);
		return sq;
	}


	int     getX() { return x; }
	int     getY(){ return y; }
	string getName() { return name; }
	double getLength(){ return leng; }
};

int main(void) {
	int t;
	cin >> t;
	
	Klasa* TabOb = new Klasa[t];
	for (int i = 0; i < t; i++){
		string name;
		int    x, y;
		cin >> name;
		cin >> x;
		cin >> y;
		Klasa abc(name, x, y);
		TabOb[i] = abc;
	}

	for (int i = 0; i < t; i++){
		cout << TabOb[i].getName() << " " << TabOb[i].getX() << " " << TabOb[i].getY() << " " << TabOb[i].getLength() << endl;
	}
	return 0;
}
You need a default constructor to initialize the array like that.
Add this under the public part of your class definition:
 
Klasa() {}
Last edited on
ok now it all works corectly but I have to think about it cuz its really interesting, thx btw ;)
Last edited on
Topic archived. No new replies allowed.