Mostenirea, Clasele Derivat

  • Uploaded by: Patrick Johnston
  • 0
  • 0
  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Mostenirea, Clasele Derivat as PDF for free.

More details

  • Words: 203
  • Pages: 2
Moştenirea, clasele derivate, reutilizarea codului #include class CLASA //clasa de baza { int x; public: CLASA(int nr = 0) //constructorul; argumentul se initializeaza altfel nu ruleaza { x = nr; //x = 0; } int aduna_nr(int n) { return x + n; } }; class CLASA_D:CLASA //clasa derivata { int altul; public: CLASA_D(int d) //constructorul { altul = d; } int aduna_nr(int n) //reutilizarea codului { return altul + CLASA::aduna_nr(n); } }; void main(void) { CLASA a(1); CLASA_D ad(1); cout< class PATRAT { int x; int generare_patrat() { return x=x*x; } public: PATRAT(int n = 1) // constructorul; dacă argumentul nu e iniţializat error la clasa derivata { x = n; //argumentul este asignat obiectului } int functie() // metoda (functia membru public) { return generare_patrat(); } };

struct PATRAT_LIM:public PATRAT //clasa derivata si clasa de baza(PATRAT) { int limita; PATRAT_LIM(int l ) //constructorul { limita = l; } int functie() //reutilizarea functiei 'int functie()' in PATRAT_LIM { return PATRAT::functie(); //atentie la constructorul clasei de baza PATRAT } }; void main(void) { PATRAT patrat(2); PATRAT_LIM patrat_lim(10); cout<<"patrat = "<<patrat.functie()<<endl; cout<<"patrat_lim = "<<patrat_lim.functie()<<endl; }

Related Documents


More Documents from "Claudia Stefan"