Jump to content

Recommended Posts

Posted

Un prim exemplu de program in POO, stiva: cu operatiile specifice de adaugare(push) si scoatere(pop).

#include<iostream>

using namespace std;

class stiva

{

	//de tip private

	//aceasta inseamna ca orice functie care nu face parte din clasa stiva, nu va putea avea acces var. st si valoare

 int st[80];

 int valoare;

public:

	//de tip public

	void initializare();

	void push(int i);

	int pop();

//observam ca metodele initializare, push, pop sunt membre ale clasei stiva, deci ele pot avea acces la "st" si la "valoare"

};

void stiva::initializare()  //atunci cand le descriem in afara clasei folosim operatorul de rezolutie ::

{

 valoare=0;

}

void stiva::push(int i)

{

if(valoare==80) //daca stiva este plina afiseaza un mesaj de eroare

cout<<"Stiva plina!!!";

else

st[valoare]=i;

valoare++;

}

int stiva::pop()

{


	if(valoare==0) //daca stiva este vida, afiseaza un mesaj de eroare

	{

		cout<<"Stiva este vida!!";

	  return 0;

	}

	else

 valoare--;

	return st[valoare];

}

int main()

{

stiva stiva1, stiva2; //creeaza doua obiecte de tip stiva

stiva1.initializare();

stiva2.initializare();

stiva1.push(3);

stiva1.push(5);

stiva1.push(11);

stiva1.push(4);

stiva2.push(8);

stiva2.push(2);

stiva2.push(7);

cout<<stiva1.pop()<<" ";

cout<<stiva1.pop()<<" ";

cout<<stiva1.pop()<<" ";

cout<<endl;

cout<<stiva2.pop()<<" ";

cout<<stiva2.pop()<<" ";

cout<<stiva2.pop()<<" ";

system("PAUSE");

return 0;

}

  • Upvote 3

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.