Declararea completă a unei clase


Listing RECT.H.

// Begin Rect.h
#include <iostream.h>
class Point //se poate schimba class in struct
// holds x,y coordinates
{
// no constructor, use default
public:
. void SetX(int x) { itsX = x; }
void SetY(int y) { itsY = y; }
int GetX()const { return itsX;}
int GetY()const { return itsY;}
private:
int itsX;
int itsY;
}; // end of Point class declaration


class Rectangle //se poate schimba class in struct
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle () {}

int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }

Point GetUpperLeft() const { return itsUpperLeft; }
Point GetLowerLeft() const { return itsLowerLeft; }
Point GetUpperRight() const { return itsUpperRight; }
Point GetLowerRight() const { return itsLowerRight; }

void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {
itsUpperRight = Location;}
void SetLowerRight(Point Location) {
itsLowerRight = Location;}

void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom = bottom; }
void SetRight (int right) { itsRight = right; }

int GetArea() const;

private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
// end Rect.hpp

Listing RECT.CPP.

// Begin rect.cpp
#include "rect.hpp"
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;

itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);

itsUpperRight.SetX(right);
itsUpperRight.SetY(top);

itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);

itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}


// compute area of the rectangle by finding corners,
// establish width and height and then multiply
int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}

int main()
{
//initialize a local Rectangle variable
Rectangle MyRectangle (100, 20, 50, 80 );

int Area = MyRectangle.GetArea();

cout << "Area: " << Area << "\n";
cout << "Upper Left X Coordinate: ";
cout << MyRectangle.GetUpperLeft().GetX();
return 0;
}


Acest cod genereaza pe ecranul terminalului urmatoarele

 
Area: 3000
Upper Left X Coordinate: 20

Cum se alege un proiect de consolă īn mediul Borland 6.0

Pentru a crea un proiect pentru consolă īn Borland 6.0 trebuie dat
New->Other
Apoi īn fereastra Tabului New se face dublu click pe Console wizard.
Va apare fereastra

unde trebuie selectat tipul surse C++ si bifata numai caseta Console Application, exact ca īn figură.
După ce se apasă butonul OK īn fereastra de editare va apare conţinutul următor

//--------------------------------------------------------

#pragma hdrstop

//--------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
return 0;
}
//--------------------------------------------------------

Se poate şterge tot şi introduce sursa dorită.

Un exemplu ceva mai complex

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class Sir{
char * text;
public:
Sir (char * sir); //constructor
~Sir() {delete text;}//destructor
int comp(Sir & a, Sir & b);
int comp(Sir & a, Sir & b, unsigned n);
void operator || (Sir & s) {strcat(text, s.text);}
void operator = (char * ch){text=ch;}
char * operator + (Sir & s){
char * tmp = (char *)malloc(strlen(text)+strlen(s.text)+1);
strcpy(tmp, text);strcat(tmp, s.text);
free(tmp);
return tmp;
}
char * operator + (char * s) {
char * tmp = (char *)malloc(strlen(text)+strlen(s)+1);
strcpy(tmp, text);strcat(tmp, s);
free(tmp);
return tmp;
}
void wr(char * mes =""){
printf("%s = %s\n", mes, text);
}
};
Sir::Sir(char * sir){
text = new char[strlen(sir)];
strcpy(text, sir);
}
int Sir::comp(Sir & a, Sir & b){
return strcmp(a.text, b.text);
}
int Sir::comp(Sir & a, Sir & b, unsigned n){
return strncmp(a.text, b.text, n);
}
void main (void){
Sir s1("abc"), s2("abcd"), s3(""), s4(" altceva");
printf("acum text este %s si se aloca pentru el %d\n","abc",sizeof(s1));
printf("acum text este %s si se aloca pentru el %d\n","abcd",sizeof(s2));

s1.wr("Sirul s1 este: ");
s2.wr("Sirul s2 este: ");
int rez1, rez2;
rez1=s1.comp(s1, s2);
printf("rez1 = %d\n",rez1);
rez2=s2.comp(s1, s2, 3);
printf("rez2 = %d\n",rez2);
s1 || s2;
s1.wr("s1 dupa concatenare este");
char * tmp="ceva";
s3 = tmp;
s3.wr("s3 este");
char * nou=" nou";
s4=s3+s4;
s4=s4+nou;
s4.wr("s4 este");
s4="inca";
s4=s4 + " inca";
s4.wr("s4 este");
//s3=s3+" altceva";
//s3.wr("s3 este");
}

Mostenirea simpla

#include <iostream.h>
enum RASA { CANIS, LUP, CIOBANESC, DOBERMAN, SORECAR};

class Mamifer
{
public:
// constructori
Mamifer();
~Mamifer();

//accesorii
int GetAge()const;
void SetAge(int);
int GetWeight() const;
void SetWeight();

//Alte metode
void Speak();
void Sleep();


protected:
int itsAge;
int itsWeight;
};

class Dog : public Mamifer
//aici se declara clasa Dog ca derivand din clasa Mamifer
{
public:

// Constructors
Dog();
~Dog();

// Accessors
BREED GetBreed() const;
void SetBreed(BREED);

// Other methods
// WagTail();
// BegForFood();

protected:
BREED itsBreed;
};

Restrictionarea accesului la membrii unei clase. Exemple

Un friend al unei clase class X este o functie sau o clasa care, cu toate ca nu este membra a acelei clase, are acces complet la membrii private si protected ai clasei X.

Exemplu de acces la variabilele private se protected dintr-o functie declarata friend

#include <conio.h>
#include <stdio.h>
int aa,bb;
class exProt{
friend int AxB(exProt o);
protected:
int A;
private:
int B;
public:
void iniAB(int a, int b){
A=a;B=b;
}
}; //vrea neaparat ; aici

int AxB(exProt o){
aa=o.A;
bb=o.B;
return o.A*o.B;
}

main(){
exProt ob;
ob.iniAB(7,8);
clrscr();
printf("%d x %d = %d",aa,bb,AxB(ob));
}

genereaza la consola text

7 x 8 = 56

Exemplu de acces la membrii protected din subclase (clase derivate)

#include <conio.h>
#include <stdio.h>
int aa,bb;
class exProt{
friend int AxB(exProt o);
protected:
int A;
//private:
int B;
public:
void iniAB(int a, int b){
A=a;B=b;
}
}; //vrea neaparat ; aici

int AxB(exProt o){
aa=o.A;
bb=o.B;
return o.A*o.B;
}
class fiu_exProt : public exProt{
friend int AxB_(fiu_exProt o);
};
int AxB_(fiu_exProt o){
aa=o.A;
bb=o.B; //genereaza eroarea exProt::B is not accesible 
//daca B ramane private
return o.A*o.B;//aceeasi eroare aici
}

main(){
exProt ob;
fiu_exProt f_ob;
ob.iniAB(7,8);

clrscr();
printf("%d x %d = %d\n",aa,bb,AxB(ob));
f_ob.iniAB(9,7);
printf("%d x %d = %d\n",aa,bb,AxB_(f_ob));

}


care generează la ieşire

7 x 8 = 56
9 x 7 = 63

Exemplu de acces la variabilele private si protected dintr-o clasa declarata friend


#include <conio.h>
#include <stdio.h>
int aa,bb;
class exProt{
friend int AxB(exProt o);
friend class friend_exProt;
protected:
int A;
private:
int B;
public:
void iniAB(int a, int b){
A=a;B=b;
}
}; //vrea neaparat ; aici

int AxB(exProt o){
aa=o.A;
bb=o.B;
return o.A*o.B;
}
class friend_exProt{
public:
int AxB_(exProt o);
};
int friend_exProt::AxB_(exProt o){
aa=o.A;
bb=o.B;
return o.A*o.B;
}

main(){
exProt ob;
friend_exProt f_ob;
ob.iniAB(7,5);

clrscr();
printf("%d x %d = %d\n",aa,bb,AxB(ob));
ob.iniAB(3,7);
printf("%d x %d = %d\n",aa,bb,f_ob.AxB_(ob));

}


Genereaza la consola text

7 x 5 = 35
3 x 7 = 21

 

© Cornel Mironel Niculae, 2003-2004

03-Mar-2005