Vous êtes sur la page 1sur 15

Ministerul Educaiei al Republicii Moldova

Universitatea Tehnic a Moldovei

Catedra: Filiera Anglofona

RAPORT
Lucrare de laborator Nr.2
la Limbajul de programare c++

A efectuat:

st. gr. FAF-141


Botnari Nicolae

A verificat:

dr., conf.univ.
M. Kulev

Chiinu 2015
0

Laboratory work No.2


Subject: Builder - depending on the class object initialization
Aim of the study:
Study the principles of definition and use of builders
Study principles and use of the destructorS
Study the types of builders
Variant 3: Stack and matrix

Basic notions of theory:


C++ Class Definitions:
When you define a class, you define a blueprint for a data type. This doesn't actually define any data,
but it does define what the class name means, that is, what an object of the class will consist of and
what operations can be performed on such an object.
A class definition starts with the keyword class followed by the class name; and the class body,
enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list
of declarations. For example, we defined the Box data type using the keyword class as follows:
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

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.

Define C++ Objects:


A class provides the blueprints for objects, so basically an object is created from a class. We declare
objects of a class with exactly the same sort of declaration that we declare variables of basic types.
Following statements declare two objects of class Box:
Box Box1;

// Declare Box1 of type Box

Box Box2;

// Declare Box2 of type Box

Both of the objects Box1 and Box2 will have their own copy of data members.

Accessing the Data Members:


The public data members of objects of a class can be accessed using the direct member access
operator (.). Let us try the following example to make the things clear:
#include <iostream>

using namespace std;

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;

// Declare Box1 of type Box

Box Box2;

// Declare Box2 of type Box

double volume = 0.0;

// Store the volume of a box here

// 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

volume = Box2.height * Box2.length * Box2.breadth;


cout << "Volume of Box2 : " << volume <<endl;
return 0;
}

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.

Classes & Objects in Detail:


So far, you have got very basic idea about C++ Classes and Objects. There are further interesting
concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed
below:
Concept

Description

Class member functions

A member function of a class is a function that has its definition or


its prototype within the class definition like any other variable.

Class access modifiers

A class member can be defined as public, private or protected. By


default members would be assumed as private.

Constructor & destructor

A class constructor is a special function in a class that is called


when a new object of the class is created. A destructor is also a
special function which is called when created object is deleted.

C++ copy constructor

The copy constructor is a constructor which creates an object by


initializing it with an object of the same class, which has been
created previously.

C++ friend functions

A friend function is permitted full access to private and protected


members of a class.

C++ inline functions

With an inline function, the compiler tries to expand the code in


the body of the function in place of a call to the function.

The this pointer in C++

Every object has a special pointer this which points to the object
itself.

Pointer to C++ classes

A pointer to a class is done exactly the same way a pointer to a


structure is. In fact a class is really just a structure with functions
in it.

Static members of a class

Both data members and function members of a class can be


declared as static.

1. Analysis of data and functions from Stack class and


matrix class
Class stack:
a composed variable of integer type, the address of the one-dimensional array in
which is stored the elements of stack.
tos simple variable of integer type, the number of elements in stack,
Class Matrix:
p simple variable of pointer to pointer double type, the address of the vector in
which is stored.
M,n simple variable of integer type, the nr of rows and colomns;

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(int row,int col)


Parameters:
row: simple variable of integer type,the element of rows
col: simple variable of integer type,the element of columns
# Destructor

~ 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

matrix productn (int n);


Returning type: matrix type, the matrix obtained after product with a number
Parameters:
n: simple variable of integer type,the nr. That is used on product
#Function for obtaining the sum of 2 matrix

matrix addmatrix (matrix m2);


Returning type: matrix type, the matrix obtained after addition
Parameters:
m2: composed variable of matrix type,first matrix used for suming
#Function for obtaining the substraction of 2 matrix

matrix minmatrix (matrix m2);


Returning type: matrix type, the matrix obtained after subtraction
Parameters:
m2: composed variable of matrix type,first matrix used for substratuioc
#Function for obtaining the substraction of 2 matrix

matrix minmatrix (matrix m2);


Returning type: matrix type, the matrix obtained after subtraction
5

Parameters:
m2: composed variable of matrix type,first matrix used for substration
#Function for obtaining the production of 2 matrix

friend matrix product (matrix a, matrix b);


Returning type: matrix type, the matrix obtained after product
Parameters:
a: composed variable of matrix type,first matrix used for product
b: composed variable of matrix type,first matrix used for product
Result for problem A:

Result for problem B :

Conclusion and Veification:


A constructor is responsible for preparing the object for action, and in particular establishing
initial values for all its data, i.e. its data members. Although it plays a special role, the
constructor is just another member function, and in particular can be passed information via its
argument list that can be used to initialise it. The name of the constructor function is the name of
the class; that's how C++ knows its a constructor.

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

Vous aimerez peut-être aussi