Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // k5.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <ctime>
- using namespace std;
- class B {
- int wartosc;
- public:
- void set(int);
- int& get();
- };
- class A {
- B *b;
- int rozmiar;
- public:
- A(int = 5);
- const int getRozmiar();
- friend ostream& operator<<(ostream&, const A&);
- friend istream& operator>>(istream &in, A &prawy);
- int& operator[](int);
- int& operator()(int);
- A& operator=(A&);
- A* operator->();
- };
- A::A(int r)
- {
- rozmiar = r;
- b = new B[rozmiar];
- for (int i = 0; i < rozmiar; i++)
- {
- b[i].set(rand() % (9) + 1);
- }
- }
- const int A::getRozmiar()
- {
- return rozmiar;
- }
- void B::set(int w)
- {
- wartosc = w;
- }
- int& B::get()
- {
- return wartosc;
- }
- int& A::operator[](int index)
- {
- return b[index].get();
- }
- int& A::operator()(int index)
- {
- return b[rozmiar - index - 1].get();
- }
- A& A::operator=(A &prawy)
- {
- int max = rozmiar > prawy.rozmiar ? prawy.rozmiar : rozmiar;
- for (int i = 0; i < max; i++)
- {
- b[i].set(prawy.b[i].get());
- }
- return *this;
- }
- A* A::operator->()
- {
- return this;
- }
- ostream& operator<<(ostream &out, const A &prawy)
- {
- for (int i = 0; i < prawy.rozmiar; i++)
- {
- out << prawy.b[i].get() << (i != prawy.rozmiar - 1 ? ", " : "");
- }
- return out;
- }
- istream& operator>>(istream &in, A &prawy)
- {
- cout << "Podaj " << prawy.rozmiar << " wartosci" << endl;
- int w;
- for (int i = 0; i < prawy.rozmiar; i++)
- {
- in >> w;
- prawy.b[i].set(w);
- }
- return in;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand(time(NULL));
- A a, b;
- cout << a << endl;
- cin >> a;
- cout << a << endl;
- a[1] = 50;
- cout << "a[1] = " << a[1] << endl;
- cout << a << endl;
- cout << "Rozmiar tablicy obiektu b wynosi: " << (b = a)->getRozmiar() << endl;
- cout << "obiekt b: " << b;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment