interhance

hey guys iam a bit stuck with some program that i try to solve

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  #pragma once
#include<iostream>
using namespace std;
#include<iostream>
#pragma once
template <class X, class Y>
class point {
private:                                                
	X x;
	Y y;

public:
	point(X a, Y b);                                 
	point();
	point(const point &);
	void setX(X a);
	void setY(Y b);
	X getX();
	Y getY();
	void print()const;
	point operator+(point po);
    point operator++();
	point operator+(int);
	point operator--();
	bool operator==(point obj);
	
};
template<class X, class Y>                            
point<X, Y>::point(X a, Y b) {
	x = a;
	y = b;

}
template<class X, class Y>                         
point <X, Y > ::point() {
	x = y = 0;

}
template<class X, class Y>                                      
point<X, Y>::point(const point &o) {
	x = o.x;
	y = o.y;

}
template<class X, class Y>                                 
void point<X, Y>::setX(X a) {
	x = a;
}
template<class X, class Y>                                
void point<X, Y>::setY(Y b) {
	y = b;
}
template<class X, class Y>                             
X point<X, Y>::getX() {
	return x;
}
template<class X, class Y>                      
Y point<X, Y>::getY() {
	return y;
}
template<class X, class Y>                                        
void point<X, Y>::print() const{
	cout << "(" << x << "," << y <<")"<< endl;
}
template<class X,class Y>                                
 point<X,Y> point<X, Y>::operator+(point po) {
	point temp;
	temp.x = x + po.x;
	temp.y = y + po.y;
	return temp;
}
template<class X,class Y>                                                     
point<X, Y>  point<X, Y>::operator++() {
	x++;
	y++;
	return *this;
}
template<class X,class Y>                               
point<X, Y> point<X, Y>::operator+(int n) {
	point temp;
	temp.x = x + n;
	temp.y = y + n;
	return temp;

}
template<class X,class Y>                                         
point<X, Y> point<X, Y>::operator--() {
	x--;
	y--;
	return *this;
}
template<class X,class Y>                                                   
bool point<X, Y>::operator==(point obj) {
	return (x == obj.x && y ==obj.y);
}
#include<iostream>
#include"Header.h"
using namespace std;
int main() {
	point<int, double>point1(1, 2.0);                       
	point<int ,double> point2;                            
	point <int, double>point3(5, 3.1);                    
	point2 = point1;                                       
	point2 = point1 + point3;
	point <int, int>point4(1, 2);
	++point4;
	point<int, int>point5;
	point5.setX(11);
	point5.setY(4);
	point<double, double>point6(5.1, 4.3);
	//point6 = point6 + 1.1;
	point<int, int>point7(1, 1);
	point<int, int>point8(2, 1);
	if (point7 == point8)
		cout << "EQUAL";
	
	else 
		cout << "not equal";
	
	system("pause");
	return 0;
}


now i want to build interhance class plot
1
2
3
4
5
6
7
class plot:public point{
int size;
point *ptr;
public:
plot(int size){
ptr=new size(s)? ///how i make a plot contractor that recive points.
}

like in main i can write
plot a<p1,p2>
Last edited on
First, what is the point of a plot being a point? A plot can have points, just like a house can own human people, even when a house is not a human.


You can make a concrete class:
class Foo : public Bar<int>
or a template class
1
2
template <typename T>
class Foo : public Bar<T>

its like
point<in,int>p1<3,2>
point<int,double>p2<1.1,2,3>
plot a<p1,p2>?
how do i do the
plot contractur?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T>
class plot:public plot<T>{
private:
	int size;
	point *ptr;
public:
	plot(int)

};
template<class T>
plot::plot(int s) {
	size = s;
	ptr = new int (s)? //forgot ..but i want to open a plot that recive the numver of size of point s
}
Last edited on
You probably want runtime polymorphism:
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
#include<iostream>
#include <vector>
#include <memory>

struct B {
  virtual void bar() = 0;
  virtual ~B() = default;
};

struct D : B
{
    D() { std::cout << "D::D\n";  }
    ~D() { std::cout << "D::~D\n";  }
    void bar() override { std::cout << "D::bar\n";  }
};

template <typename T>
struct E : B
{
    T x;
    E( T x ) : x(x) { std::cout << "E::E " << x << '\n';  }
    ~E() { std::cout << "E::~E " << x << '\n';  }
    void bar() override { std::cout << "E::bar " << x << '\n';  }
};

int main() {
    std::vector<std::unique_ptr<B>> v;
    v.push_back(std::make_unique<E<int>>(42));
    v.push_back(std::make_unique<D>());
    v.push_back(std::make_unique<E<double>>(3.14));
    for( auto& p: v ) p->bar();
    return 0;
}
Topic archived. No new replies allowed.