Week 9
Week 9
#include <iostream>
using namespace std;
// Define a structure
struct Student {
string name;
int rollNumber;
float marks;
};
int main() {
// Create a structure variable
Student s1;
// Assign values
s1.name = "John Doe";
s1.rollNumber = 101;
s1.marks = 89.5;
// Display values
cout << "Name: " << s1.name << endl;
cout << "Roll Number: " << s1.rollNumber << endl;
cout << "Marks: " << s1.marks << endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
struct Rectangle {
int length;
int breadth;
int main() {
Rectangle r1 = {10, 5};
return 0;
}
What is a Pointer?
type *pointerName;
Example:
int *p;
#include <iostream>
using namespace std;
int main() {
int x = 10;
int *p; // Pointer to int
return 0;
}
Output:
Value of x: 10
Address of x: 0x61ff08 (example address)
Pointer p holds: 0x61ff08
Value at address stored in p (*p): 10
#include <iostream>
using namespace std;
int main() {
int a = 5;
int *ptr = &a;
return 0;
}
Output:
Before: 5
After: 20
Imagine:
a = 10
Address of a: 0x1234
p = &a
p holds: 0x1234
Copy Constructor
class Person {
string name;
int age;
public:
// Constructor
Person(string n, int a) {
name = n;
age = a;
}
// Copy Constructor
Person(const Person &p) {
name = p.name;
age = p.age;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Person person1("Alice", 25);
Person person2 (person1); // Copy constructor is called here
return 0;
}
Output:
We pass by const reference (const Person &p) to avoid unnecessary copying and to
protect the source object.
Example:2
#include <iostream>
using namespace std;
class Student {
public:
int age;
// Constructor
Student(int a) {
age = a;
}
// Copy Constructor
Student(const Student &s) {
age = s.age;
}
void display() {
cout << "Age: " << age << endl;
}
};
int main() {
Student s1(20); // normal constructor
Student s2 = s1; // copy constructor
s1.display(); // Age: 20
s2.display(); // Age: 20
return 0;
}
To copy objects properly (especially if the object has pointers or dynamic memory).
To pass objects by value to functions.
To return objects from functions.
A shallow copy constructor in C++ is when you copy all the fields of an object exactly as they
are, including pointers — just copying their addresses, not the actual data they point to.
If an object has a pointer inside, both the original and the copied object will point to the same
memory.
Example
#include <iostream>
using namespace std;
class Example {
public:
int *ptr;
// Constructor
Example(int val) {
ptr = new int(val);
}
void display() {
cout << "Value: " << *ptr << endl;
}
};
int main() {
Example e1(10);
Example e2 = e1; // shallow copy
e1.display(); // Value: 10
e2.display(); // Value: 10
e1.display(); // Value: 20
e2.display(); // Value: 20 (e2 also changed!)
return 0;
}
Note:
If you delete one object, the pointer becomes invalid for the other object.
It can cause program crashes.
Deep copy creates a new memory and copies the actual data — so both objects are
fully independent.
Deep copy avoids the problems shallow copy causes.
#include <iostream>
}
student(int id,string name,int temp)
{
this->id=id;
this->name=name;
this->x=new int;
*(this->x)=temp;
}
student(student &object)
{
this->id=object.id;
this->name=object.name;
this->x=new int;
*(this->x)=*(object.x);
void display()
{
cout<<" name"<<name<<endl;
cout<<" id"<<id<<endl;
cout<<" ptr "<<x<<endl;
}
~student()
{
cout<<" destructor is called"<<endl;
}
};
int main()
{
cout << "Hello world!" << endl;
student ob1(123,"abc",678);
student ob2(ob1);
cout<<" display for obj1 "<<endl;
ob1.display();
cout<<" display obj2 "<<endl;
ob2.display();
return 0;
}
Output:
Hello world!
display for obj1
nameabc
id123
ptr 0x2d8c6c0
display obj2
nameabc
id123
ptr 0x2d8c6e0
destructor is called
destructor is called
LAB TASKS
Lab Task 01:
Title (string)
Author (string)
ISBN number (integer)
Price (float)
Scenario:
The library wants a basic system where the librarian can enter details of a book and then view
them.
Your task:
Example Output:
Book Title: The Great Gatsby
Author: F. Scott Fitzgerald
ISBN: 123456789
Price: 299.99
LAB Task 02:
You are working as a developer for a temperature monitoring system. The system measures the
current temperature in a machine. Sometimes, based on conditions (like cooling or heating),
the temperature needs to be modified indirectly using pointers. You are asked to:
1. Create an integer variable called temperature and assign it an initial value (e.g., 75).
2. Create a pointer that stores the address of temperature.
3. Display the current temperature using the pointer.
4. Simulate a cooling system by decreasing the temperature by 10 using the pointer.
5. Display the updated temperature.
Example Output:
Current Temperature: 75
Temperature via Pointer: 75
Temperature after Cooling: 65