0% found this document useful (0 votes)
2 views

Lab2

The document contains two C++ class implementations: one for a 3x3 matrix that includes methods for input, display, and addition of two matrices using a friend function, and another for a Counter class that tracks its instances, allows for incrementing its value, and provides a method to retrieve the current count of objects. The Matrix class initializes its elements to zero and performs addition through a separate function, while the Counter class features overloaded increment operators and maintains a static count of its instances. Both classes are demonstrated in a main function that allows user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab2

The document contains two C++ class implementations: one for a 3x3 matrix that includes methods for input, display, and addition of two matrices using a friend function, and another for a Counter class that tracks its instances, allows for incrementing its value, and provides a method to retrieve the current count of objects. The Matrix class initializes its elements to zero and performs addition through a separate function, while the Counter class features overloaded increment operators and maintains a static count of its instances. Both classes are demonstrated in a main function that allows user interaction.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2. Define a class matrix with an integer array of 3X3 as a data member.

Define a friend function which


adds two matrix objects and returns resultant object.

Program:

#include <iostream>

using namespace std;

class Matrix {

private:

int mat[3][3];

public:

Matrix() {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

mat[i][j] = 0;

void inputMatrix() {

cout << "Enter matrix elements (3x3):" << endl;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

cin >> mat[i][j];

void displayMatrix() const {


for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

cout << mat[i][j] << " ";

cout << endl;

friend Matrix addMatrix(const Matrix& m1, const Matrix& m2);

};

Matrix addMatrix(const Matrix& m1, const Matrix& m2) {

Matrix result;

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

result.mat[i][j] = m1.mat[i][j] + m2.mat[i][j];

return result;

int main() {

Matrix mat1, mat2, result;

cout << "Matrix 1:" << endl;

mat1.inputMatrix();

cout << "Matrix 2:" << endl;

mat2.inputMatrix();

result = addMatrix(mat1, mat2);


cout << "Resultant Matrix after addition:" << endl;

result.displayMatrix();

return 0;

1. Develop a class Counter that represents a simple integer counter. The class should satisfy the
following requirements:

(a) A he prefix increment and postfix increment operators should be overloaded in order t constructor
should be provided that takes a single int argument that is used to initialize the counter value. The
argument should default to zero.

(b) To provide a means by which to increment the counter value.

(c) A member function getValue should be provided that returns the current counter value.

In addition, the class must track how many Counter objects are currently in existence. A means for
querying this count should be provided. The code must not use any global variables. (Hint: Use static
members.)

#include <iostream>

using namespace std;

class Counter {

private:

int value;

static int objectCount;

public:

Counter(int initialValue = 0) : value(initialValue) {

objectCount++;

Counter(const Counter& other) : value(other.value) {

objectCount++;
}

~Counter() {

objectCount--;

Counter& operator++() {

++value;

return *this;

Counter operator++(int) {

Counter temp = *this;

++value;

return temp;

int getValue() const {

return value;

static int getObjectCount() {

return objectCount;

};

int Counter::objectCount = 0;

int main() {

int initialValue;

char choice;

do {
cout << "Enter an initial value for the Counter (default is 0): ";

cin >> initialValue;

Counter c(initialValue);

cout << "Initial value of the Counter: " << c.getValue() << endl;

cout << "Value after prefix increment: " << (++c).getValue() << endl;

cout << "Value after postfix increment: " << (c++).getValue() << endl;

cout << "Total Counter objects: " << Counter::getObjectCount() << endl;

cout << "Do you want to create another Counter? (y/n): ";

cin >> choice;

} while (choice == 'y' || choice == 'Y');

cout << "Total Counter objects remaining: " << Counter::getObjectCount() << endl;

return 0;

You might also like