Warning
This page is located in archive.

2 - Třídy a objekty

Jednoduchá třída

#include <iostream>
 
using namespace std;
 
class TridaA
{
	int a;		// implicitne private
 
	public:
		int b;
		TridaA();	// konstruktor
		~TridaA();
	private:
		int c;
};
 
TridaA::TridaA()
{
	cout << "konstruktor" << endl;
}
 
TridaA::~TridaA()
{
	cout << "destruktor" << endl;
}
 
struct TridaB
{
	int a;		// imlicitne public
};
 
int main ()
{
	TridaA a;
	TridaB b;
 
        // promenna a je private
	a.a = 10;	
	cout << a.a << endl;
 
        // promenna a je public
        b.a = 10;
	cout << b.a << endl;
 
	TridaA *c = new TridaA();
	c->b = 88;
	delete c;
 
	return 0;
}

Třída s metodami

#include <iostream>
 
class TridaA
{
	private:
		int stav;
 
	public:
		TridaA () {}
		TridaA (int a) { }
		TridaA (double a) { }
		void metoda ()
		{
                        // inline definice metody
			// ekvivalent void TridaA::metoda () { ... }
			// definice metody
			std::cout << "metoda A" << std::endl;
		}
		int ctiStav ();
};
 
void TridaA::ctiStav ()
{
	return stav;
}
 
class TridaB : public TridaA
{
/*
dedeni: 
	public: zachova se viditelnost podle puvodniho predpisu
	protected: public -> protected
	private: vsechno je private
*/
	public:
		void metoda ()
		{
			std::cout << "metoda B" << std::endl;
			std::cout << "volani metody TridaA::metoda() "<< std::endl;
			TridaA::metoda ();
		}
};
 
int main ()
{
	TridaA a;
	a.metoda ();
 
	TridaA b(10);
 
	TridaA c(3.14);
 
	TridaB d;
	d.metoda ();
	d.TridaA::metoda();
 
	return 0;
}

courses/bd5b37ppc/tutorials/02.txt · Last modified: 2019/10/03 20:41 by viteks