0% found this document useful (0 votes)
29 views6 pages

C++ Programming Concepts

The document contains multiple lab tasks related to C++ programming, including the use of the 'this' keyword in a Rectangle class, explanations of shallow and deep copy, and the implementation of static members in a Car class to track the number of objects. It also includes examples of static member functions in a MathUtils class for performing arithmetic operations. Each task demonstrates different object-oriented programming concepts in C++.

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

C++ Programming Concepts

The document contains multiple lab tasks related to C++ programming, including the use of the 'this' keyword in a Rectangle class, explanations of shallow and deep copy, and the implementation of static members in a Car class to track the number of objects. It also includes examples of static member functions in a MathUtils class for performing arithmetic operations. Each task demonstrates different object-oriented programming concepts in C++.

Uploaded by

Awais Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Task # 01: this keyword

#include <iostream>
using namespace std;

class Rectangle {
private:
int length;
int width;
public:
//methods
void setDimentions(int length, int width) {
this->length = length;
this->width = width;
}

void getDimentions() const{


cout << "Length: " << length << ", Width: " << width << endl;
}
};

int main() {
Rectangle r1;

r1.setDimentions(20, 15);
r1.getDimentions();

return 0;
}

Lab Task # 02: copy constructor


Write short answer in your own words about the difference between the deep copy and
shallow copy.
Shallow copy:
Shallow copy refers to the bit by bit copying of one object to another. This
copying process doesn’t care for any dynamically allocated members. It’s just one to one copy.
Deep copy:
Deep copy is perfect for dynamically allocated variables because this is more
efficient. Unlike shallow copy, deep copy does all the copying process by keeping care of every
datatype and related memory allocation.

Lab Task # 03: Static member


Tracking Objects:
#include <iostream>

using namespace std;

class Car {

private:

// Static data member to track the total number of Car objects

static int totalCars;

public:

// Constructor

Car() {

totalCars++; // Increment totalCars when a new object is created

cout << "A car has been created. Total cars: " << totalCars << endl;

// Destructor

~Car() {

totalCars--; // Decrement totalCars when an object is destroyed

cout << "A car has been destroyed. Total cars: " << totalCars << endl;

// Static function to get the value of totalCars

static int getTotalCars() {

return totalCars;

};

// Initialize the static data member

int Car::totalCars = 0;

int main() {

cout << "Starting the program...\n";


// Create and destroy car objects to test the class

Car car1;

Car car2, car3;

cout << "Inside the block, total cars: " << Car::getTotalCars() << endl;

} // car2 and car3 go out of scope here

cout << "Back in main, total cars: " << Car::getTotalCars() << endl;

return 0;

Static Member Functions:


#include <iostream>

using namespace std;

class Car {

private:

// Static data member to track the total number of Car objects

static int totalCars;

public:

// Constructor

Car() {

totalCars++; // Increment totalCars when a new object is created

cout << "A car has been created. Total cars: " << totalCars << endl;

// Destructor

~Car() {
totalCars--; // Decrement totalCars when an object is destroyed

cout << "A car has been destroyed. Total cars: " << totalCars << endl;

// Static member function to get the value of totalCars

static int getTotalCars() {

return totalCars;

};

// Initialize the static data member

int Car::totalCars = 0;

int main() {

cout << "Creating car objects...\n";

Car car1, car2;

// Calling getTotalCars using the class name

cout << "Total cars (using class name): " << Car::getTotalCars() << endl;

// Calling getTotalCars using an object

cout << "Total cars (using object): " << car1.getTotalCars() << endl;

Car car3;

cout << "Total cars inside block: " << Car::getTotalCars() << endl;

} // car3 goes out of scope here

cout << "Total cars after block: " << Car::getTotalCars() << endl;

return 0;
}

Utility Function:
#include <iostream>

using namespace std;

class MathUtils {

public:

// Static member function to add two integers

static int add(int a, int b) {

return a + b;

// Non-static member function to square a number

int square(int x) {

return x * x;

};

int main() {

// Calling the static function using the class name

int sum = MathUtils::add(10, 20);

cout << "Sum (using class name): " << sum << endl;

// Creating an object to access the non-static function

MathUtils mathUtils;

int result = mathUtils.square(5);

cout << "Square of 5 (using object): " << result << endl;

// Calling the static function using an object (allowed but not recommended)

int sumViaObject = mathUtils.add(15, 25);


cout << "Sum (using object): " << sumViaObject << endl;

return 0;

You might also like