0% found this document useful (0 votes)
11 views5 pages

Lab Reports OPP

The document discusses tasks from a lab assignment on object-oriented programming. Task 1 involves adding a User class with data members for name, account number, and balance. Task 2 explains the difference between declaring an object vs pointer to an object and when to use delete. Task 3 shows updated code with a default and parameterized User constructor.
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)
11 views5 pages

Lab Reports OPP

The document discusses tasks from a lab assignment on object-oriented programming. Task 1 involves adding a User class with data members for name, account number, and balance. Task 2 explains the difference between declaring an object vs pointer to an object and when to use delete. Task 3 shows updated code with a default and parameterized User constructor.
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/ 5

Object oriented Programing_EE_C Course Instructor:

Lab Engineer: Sana Fatima

[Type here] [Type here]


LAB 3 :
TASK 1:

Task 1: add another class in the same code named USER .data members should be

Name;

Account number;

Account Balance ;

CODE:

Header file:
#include<iostream>
#include<string>
using namespace std;

class user
{ public:
string name;
int accnumber;
int balanceamount;

void initializeuserdata(string ,int,int);//warning green


void displayuserdetails();

};

SOURCE FILE :
#include<iostream>
#include<string>
using namespace std;
#include"user.h"

void user::initializeuserdata(string name, int accnumber, int balanceamount)


{
this->name = name;
this->accnumber = accnumber;
this->balanceamount = balanceamount;
}

void user::displayuserdetails()
{
cout << "the name of the user is " << name << endl;
cout << "the account number is :" << accnumber << endl;
cout << "the account balance is:" << balanceamount << endl;
Object oriented Programing_EE_C Course Instructor:
Lab Engineer: Sana Fatima

[Type here] [Type here]


}

MAIN CPP FILE :


#include<iostream>
#include"bank.h"
#include"user.h"

int main()
{
/*bank b1;
bank *b2;*/

/*b1.initializebankdata(345);
b1.displaybankdetails();
b2 = new bank();*/
user u1;

u1.initializeuserdata("mansoor",4545,34443);
u1.displayuserdetails();

//bank::bank()
//
//{

// bankcode = 0;
// cout<<"hello i am the constructor"

//}///constructors are made to initialize objects

///*b2->initializebankdata(445);

// b2->displaybankdetails();*/
return 0;
}

OUTPUT:
Object oriented Programing_EE_C Course Instructor:
Lab Engineer: Sana Fatima

[Type here] [Type here]

Task : 2

Task 2 was difference between class obj1; and class *obj2;

And why we can't delete class obj1 and can delete class *ob2 (related to dynamic memory allocation)

ANS: In our code, `user u1;` creates an object of the `user` class, and `user *u2;` declares a pointer to an
object of the `user` class. Let's break down the differences:

1. `user u1;`: This creates an object of the `user` class on the stack. This object has a fixed memory
allocation and exists within the scope where it's declared. It gets automatically destroyed when it goes
out of scope (in this case, when the `main` function ends). You don't need to explicitly delete it because
it's not allocated on the heap.
Object oriented Programing_EE_C Course Instructor:
Lab Engineer: Sana Fatima

[Type here] [Type here]


2. `user *u2;`: This declares a pointer to a `user` class, but it doesn't create an actual object. You can
allocate memory for a `user` object dynamically on the heap using `u2 = new user;`. Objects created on
the heap persist until explicitly deleted. This means you should use `delete u2;` to release the memory
allocated for the object when you're done with it. Failing to delete it can result in a memory leak.

Task 3:
Code : user .h files :
#pragma once
#include <string>

class User {
public:

User();
User(const std::string& name, int accNumber, int balanceAmount);

std::string name;
int accNumber;
int balanceAmount;
};

Code : user.cpp files


#include "user.h"

using namespace std;

User::User() : accNumber(0), balanceAmount(0) {}

User::User(const string& name, int accNumber, int balanceAmount)


: name(name), accNumber(accNumber), balanceAmount(balanceAmount) {}

Code : main.cpp files :


#include <iostream>
#include "user.h"

using namespace std;

int main() {
User u1;
User u2("Mansoor", 4545, 34443);

cout << "User 1 Details:" << endl;


Object oriented Programing_EE_C Course Instructor:
Lab Engineer: Sana Fatima

[Type here] [Type here]


cout << "Name: " << u1.name << endl;
cout << "Account Number: " << u1.accNumber << endl;
cout << "Balance Amount: " << u1.balanceAmount << endl;

cout << "\nUser 2 Details:" << endl;


cout << "Name: " << u2.name << endl;
cout << "Account Number: " << u2.accNumber << endl;
cout << "Balance Amount: " << u2.balanceAmount << endl;

return 0;
}

Output :

Name : Mansoor bin Tariq


DE 44 EE C
432226

You might also like