Académique Documents
Professionnel Documents
Culture Documents
record=r;
}
void PilotF1::write_nr_pole_position(int p)
{
nr_pole_position=p;
}
const char *PilotF1::get_nume()
{
return nume;
}
const char *PilotF1::get_echipa()
{
return echipa;
}
int PilotF1::get_varsta()
{
return varsta;
}
int PilotF1::get_record()
{
return record;
}
int PilotF1::get_nr_pole_position()
{
return nr_pole_position;
}
void Afisare(PilotF1 *pilot)
{
printf("Pilotul %s apartinand echipei %s avand varsta %d de ani cu un
numar de %d pole positionuri a obtinut un record de %d",pilot>get_nume(),pilot->get_echipa(),pilot->get_varsta(),pilot>get_nr_pole_position(),pilot->get_record());
2) /*Modify the 2-nd example so that the code could be launched into
execution*/
#include <iostream>
using namespace std;
class Test2
{
int x;
public:
Test2()
{
cout<<"\nApel constructor explicit vid.";
}
void write_x(int y) { x=y; }
};
int main(void)
{
Test2 ob1;
int a;
cout<<"\nIntroduceti valoarea variabilei de tip \"int\" din clasa: ";
cin>>a;
ob1.write_x(a);
return 0;
}
3) /*Starting with the code that exemplifies the matrix operations, add the
specific methods for:
- displaying the elements from the main diagonal of the matrix, in case the
matrix is square;
if not, display a significant message;
- displaying the elements which are below the secondary diagonal;
- displaying a matrix that has identical dimensions with the original matrix
and its elements
can have as possible values 0s (if the corresponding element is greater than
a certain
threshold value) or 1s (otherwise);
*/
#include <iostream>
using namespace std;
class Matrix
{
//atribute
int matrix[10][10], dim1, dim2;
public:
//constructor explicit vid
Matrix()
{
//variabile locale
int i, j;
cout<<"\nIntroduceti dimensiunile matricii: ";
cin>>dim1;
cin>>dim2;
cout<<"\nIntroduceti elementele matricii: ";
for(i=0; i<dim1; i++)
{
for(j=0; j<dim2; j++)
{
cout<<"\nmatrix["<<i<<"]["<<j<<"] = ";
cin>>matrix[i][j];
}
}
}
}
cout<<"\n";
}
void displayAnother(int v)
{
//variabile locale
int i, j, t;
cout<<"\nElementele matricii: ";
for(i=0; i<dim1; i++)
{
cout<<"\n";
for(j=0; j<dim2; j++)
{
//apelul metodei private care returneaza
valoarea unui element din matrice
t=returnElement(i, j)>v?0:1;
cout<<t<<" ";
}
}
cout<<"\n";
}
//declararea metodei de afisare a elementelor unei coloane
void displayColumn(int col);
private:
//declararea metodei de afisare a unui element
int returnElement(int row, int column);
};
//implementarea externa a metodelor (publice sau private) declarate in
clasa
void Matrix::displayColumn(int col)
{
if(col<0||col>dim2)
{
cout<<"\nColoana cu numarul "<<col<<" nu exista in
matricea din clasa!\n";
}
else
{
cout<<"\nElementele coloanei "<<col<<": ";
for(int i=0; i<dim1; i++)
{
cout<<returnElement(i, col)<<" ";
}
}
cout<<endl;
}
int Matrix::returnElement(int row, int column)
{
return matrix[row][column];
}
int main(void)
{
Matrix m1; //instantiere
int c;
m1.displayMatrix();
cout<<"\nIntroduceti un numar de coloana ale carei elemente vor fi
afisate: ";
cin>>c;
m1.displayColumn(c);
m1.displayDiagonal();
m1.displayunderDiagonal();
cout<<"\nIntroduceti o valoare de referinta pentru matrice:";
cin>>c;
m1.displayAnother(c);
//incercare (imposibila) de a accesa direct un membru privat al clasei
//m1.returnElement(0, 0);
return 0;
}
4) /*Write a C++ class that has as private variable a date field. The date is
defined as a structure
declared outside the class and it contains the fields day int, month int,
year int. The class
contains public accessor and mutator methods that are capable of using the
private
information. The class also contains two public methods that:
- test the validity of the stored date;
- write into a file all the dates from the current year that precede
chronologically the class
stored date;
In the main method, after instantiating the class and after reading from the
keyboard all the
components of a date, call the member methods and then verify the obtained
results.*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct data
{
int zi;
int luna;
int an;
};
class Calendar
{
data calend;
public:
void write_data(data cal)
{
calend.zi=cal.zi;
calend.luna=cal.luna;
calend.an=cal.an;
}
data get_data()
{
return calend;
}
int validate(data cal)
{
if((cal.zi>=1&&cal.zi<=31)&&(cal.luna>=1&&cal.luna<=12)&&(cal.an>=1&&cal.an<=1
00000)) return 1;
else return 0;
}
void write_file(const char *calea)
{
FILE *f;
if(!(f=fopen(calea,"w")))
{
printf("Eroare la crearea fisierului\n");
return;
}
for(int i=calend.luna;i<=12;i++)
{
for(int j=calend.zi;j<=31;j++)
{
fprintf(f,"%d.%d.%d\n",j,i,calend.an);
}
}
printf("Datele au fost scrise cu success!\n");
fclose(f);
}
};
int main(void)
{
Calendar cal;
data tmp;
printf("Introduceti o data in formatul dd/zz/aaaa: ");scanf("%d/%d/
%d",&tmp.zi,&tmp.luna,&tmp.an);
cal.write_data(tmp);
if(cal.validate(cal.get_data())) cal.write_file("txt.txt");
else printf("Data nu are formatul corect. Datele nu au fost scrise in
fisier.\n");
printf("Data scrisa in clasa este: %d/%d/
%d",cal.get_data().zi,cal.get_data().luna,cal.get_data().an);
return 0;
}
6) /*Write a C++ application that defines a class called Triangle. The class
contains as private
variables the triangle's sides a, b and c. The methods included in the class
will determine
whether the values of a,b and c form a triangle and will calculate the shape's
area and
perimeter. Write a distinct method that will print a specific message if the
triangle is right*/
#include <iostream>
using namespace std;
class Triunghi {
private:
float a, b, c;
public:
void verif_dreptunghic();
Triunghi(int x, int y, int z) {
a=x;
b=y;
c=z;
}
int verifica_laturi() {
if(a+b>c && a+c>b && b+c>a)
return 1;
else
return 0;
}
float calcul_arie() {
float p, arie;
p=(a+b+c)/2;
arie=sqrt(p*(p-a)*(p-b)*(p-c));
return arie;
}
int calcul_perimetru() {
int perimetru;
perimetru=a+b+c;
return perimetru;
}
};
void Triunghi::verif_dreptunghic() {
if(a*a+b*b==c*c || a*a + c*c==b*b || b*b+c+c==a*a)
/*Write a class named Safe that has as private attributes the cipher and the
amount of money.
Implement the private accessor and mutator methods getMoney() and putMoney().
The public
methods putInSafe() and getFromSafe() will call the previous methods only if
the cipher sent
as parameter matches the value stored inside the class. Display a message if
the cipher is not
correct.*/
#include <iostream>
#include<conio.h>
using namespace std;
class Seif
{
private:
float suma;
float cifru;
float get_sum ()
{
return suma;
}
void set_sum (float s)
{
suma=s;
}
public:
void puneInSeif (float summ )
{
float m=0;
set_sum(0);
m=get_sum();
set_sum(summ+m);
cout<<"\n Noua suma din cont este :"<<summ+m;
}
float scoateDinSeif (float summ1)
{
float su=0;
su=get_sum();
return summ1;
set_sum(su-summ1);
cout<<"\n Suma extrasa din cont este :"<<summ1;
cout<<"\n Noua suma din cont este :"<<su-summ1;
}
int Cifru (int p)
{
cifru=1234;
if(p!=cifru)
return 0;
else
return 1;
}
};
void main (void)
{
Seif card1; //instantiere
float pin, summ;
int x;
cout<<"\nIntroduceti pinul: "; cin>>pin;
while (card1.Cifru(pin)==0)
{
cout<<"\nParola gresita!! Reintroduceti: ";
cin>>pin;
}
cout<<"\nPentru a efectua o depunere introduceti valoarea 1, pentru a efectua
o extragere introduceti valoarea 2: ";
cin>>x;
if (x==1)
{
cout<<"\nSuma pe care doriti sa o depuneti: ";
cin>>summ;
card1.puneInSeif(summ);
}
else
if (x==2)
{
cout<<"\nSuma pe care doriti sa o scoateti: ";
cin>>summ;
card1.scoateDinSeif(summ);
}
else
{
cout<<"Introduceti din nou valoarea: ";
cin>>x;
}
getche ();
}