Jump to content

Program Stiva In Poo


skyler_sdf

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.