Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1.cpp : Defines the entry point for the console application.
- // Odnaleźć błędy i poprawić
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- #define MAX_CHAR 512
- class A
- {
- protected:
- char str[MAX_CHAR];
- public:
- A() { str[0] = '\0'; }
- A(char sstr[]) {
- str[0] = '\0';
- try {
- strcpy_s(str, sizeof(str), sstr);
- }
- catch (bad_alloc) {
- }
- }
- void disp() { cout << str << endl; }
- };
- class B
- {
- protected:
- int *b;
- public:
- B() { b = NULL; }
- B(int dim) {
- try {
- b = new int[dim];
- memset(b, 0, dim*sizeof(int));
- }
- catch (bad_alloc) {
- }
- }
- ~B() { if (b) delete[] b; }
- void disp() {
- if (b)
- for (size_t it = 0; it<_msize(b) / sizeof(b[0]); ++it)
- cout << b[it] << endl;
- }
- };
- class C : public A, public B
- {
- public:
- C(int i, char sstr[]) : B(i), A(sstr) {};
- C(const C &ob) { *this = ob; };
- int &operator [] (const size_t ind) { return b[ind]; }
- friend ostream & operator <<(ostream &wyjscie, C const &ob);
- };
- ostream & operator << (ostream & wyjscie, C const &ob)
- {
- if (ob.str)
- {
- int inc=0, size = _msize(ob.b)/sizeof(int);
- wyjscie << "int[] = ";
- while (inc != size)
- wyjscie << ob.b[inc++] << " ";
- wyjscie << "str = " << ob.str << endl;
- }
- else
- {
- exit(5);
- }
- return wyjscie;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- C ob1(10, "abcde"), ob2(20, "efkjyklmn");
- for (size_t it = 0; it<10; ++it)
- ob2[it] = it*it + 1;
- cout << ob1 << endl << ob2;
- ob1 = ob2;
- cout << endl << endl << ob1;
- C ob3 = ob1;
- cout << endl << endl << ob3;
- return 0;
- }
- #undef MAX_CHAR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement