Académique Documents
Professionnel Documents
Culture Documents
RAPORT
Lucrare de laborator Nr.2
la Limbajul de programare c++
A efectuat:
A verificat:
dr., conf.univ.
M. Kulev
Chiinu 2015
0
The keyword public determines the access attributes of the members of the class that follow it. A
public member can be accessed from outside the class anywhere within the scope of the class object.
You can also specify the members of a class as private or protected which we will discuss in a subsection.
Box Box2;
Both of the objects Box1 and Box2 will have their own copy of data members.
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main( )
{
Box Box1;
Box Box2;
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
When the above code is compiled and executed, it produces the following result:
Volume of Box1 : 210
Volume of Box2 : 1560
It is important to note that private and protected members can not be accessed directly using direct
member access operator (.). We will learn how private and protected members can be accessed.
Description
Every object has a special pointer this which points to the object
itself.
2.The functions:
Stack Functions:
# Constructor
stack::stack()
Returning type: stack
Parameters: void
# Checking if is empty
int stack::isempty()
Returning type: int type,o if is empty 1 otherwise
Parameters: void
# Checking if is full
int stack::isfull()
Returning type: int type,i if is full 0 otherwise
Parameters: void
# Pushing one element
void stack::push(int i)
Returning type: void
Parameters:
i: simple variable of integer type,the element that we want to push
# Poping one element out
void stack::pop()
Returning type: void
Parameters:
Matrix Functions:
# Default Constructor
matrix()
# Constructor
~ matrix ()
# Read the matrix
void accept();
Returning type: void
Parameters:
No parameters
# Print the matrix on the screen
void display();
Returning type: void
Parameters:
# The product of the matrix with a number
Parameters:
m2: composed variable of matrix type,first matrix used for substration
#Function for obtaining the production of 2 matrix
Bybliografy:
1. http://www.cplusplus.com/doc/tutorial/
2. https://en.wikipedia.org/wiki/C%2B%2B
3. http://www.tutorialspoint.com/cplusplus/
The Appendix A:
//Stack implementation as a class
# include<iostream>
# include<process.h>
# include<conio.h>
# define SIZE 20
using namespace std;
class stack
{
int a[SIZE];
int tos; // Top of Stack
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0; //Initialize Top of Stack
}
int stack::isempty()
{
return (tos==0?1:0);
}
int stack::isfull()
{
return (tos==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
cout<<"Stack overflow error ! Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
9
{
return(a[--tos]);
}
else
{
cout<<"Stack is empty! What to pop...!";
}
return 0;
}
int main()
{
stack s;
int ch=1,num;
while(ch!=0)
{
cout<<"Stack operations Menu:\n";
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.IsEmpty\n";
cout<<"4.IsFull\n";
cout<<"0.Exit\n";
cin>>ch;
switch(ch)
{
case 0:
return(0); //Normal Termination of Program
case 1:
cout<<"Enter the number to push: ";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again";
}
}//end of while
getch();
return 0;
}
The Appendix B:
#include <iostream>
10
#include <cstdlib>
#include <ctime>
using namespace std;
class matrix
{
double **p;
int m, n;
int error;
public:
matrix()
{
error = 0;
m=0;
n = 0;
p = NULL;
}
matrix(int row,int col)
{
error = 0;
m = row;
n = col;
p = new(double *);
for (int i = 0; i < m; i++)
p[i] = new double[n];
}
matrix(int s)
{
m = s;
n = s;
p = new(double *);
for (int i = 0; i < m; i++)
p[i] = new double[n];
}
matrix(const matrix& b){
p = new(double *);
for (int i = 0; i < m; i++)
p[i] = new double[n];
p=b.p;
m = b.m;
n = b.n;
}
~matrix()
{
for (int i = 0; i < m; i++)
delete p[i];
delete p;
}
11
void accept()
{
cout<<"\nEnter matrix elements:\n";
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> p[i][j];
}
}
}
void display()
{
cout <<"The matrix is:";
for(int i = 0; i < m; i++)
{
cout <<endl;
for(int j = 0; j < n; j++)
{
cout << p[i][j] <<" ";
}
}
}
matrix addmatrix(matrix m2)
{
if(m==m2.m && n==m2.n){
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return T;
} else {error = 1;}
}
matrix minmatrix(matrix m2)
{
if(m==m2.m && n==m2.n){
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
12
return T;
} else {error = 1;}
}
matrix productn(int n)
{
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] * n;
}
}
return T;
}
friend matrix product (matrix a, matrix b);
};
matrix product(matrix a , matrix b)
{
if(a.n == b.m)
{
matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
return T;
}
}
int main()
{
matrix a(2, 3);
matrix b(3, 2);
matrix q,s,m;
int i, j;
a.accept();
a.display();
13
b.accept();
b.display();
q = product(a,b);
q.display();
return 0;
}
14