Here I implemented a c++ program using OOP concepts. Let's say there are 1000 salesmen (1000 Salesman Class objects) in a company. If I want to print the name of each and every salesman (using a loop or something), how to do it? Not only for printing the name, let's say the company wants to find the average salary spent. It can be easy that if I can run over all objects in the class.
This is my code (I put the code here just for reference)
#include <string>
#include <iostream>
class Salesman
{
private:
int salesmanId;
string salesmanName;
int salary;
string contactNumber;
public:
Salesman();
~Salesman();
void setSalesmanDetails(int id, string name, int sal, string number);
void displaySalesmanDetails();
void setSalesmanContactNumber(string number);
};
void Salesman ::setSalesmanDetails(int id, string name, int sal, string number)
{
salesmanId = id;
salesmanName = name;
salary = sal;
contactNumber = number;
}
void Salesman ::displaySalesmanDetails()
{
cout << "Salesman ID: " << salesmanId << endl;
cout << "Salesman Name: " << salesmanName << endl;
cout << "Salary: " << salary << endl;
cout << "Contact Number: " << contactNumber << endl
<< endl;
}
void Salesman::setSalesmanContactNumber(string number)
{
contactNumber = number;
}
Salesman::Salesman()
{
}
Salesman::~Salesman()
{
}
int main(int argc, char *argv[])
{
string number;
Salesman *s1 = new Salesman;
s1->setSalesmanDetails(1, "John", 30000, "772358375");
Salesman *s2 = new Salesman;
s2->setSalesmanDetails(2, "Ann", 40000, "773029452");
Salesman *s3 = new Salesman;
s3->setSalesmanDetails(3, "Leema", 35000, "778294526");
}
Just make a class that stores a vector that holds SalesMan objects. Give that class the ability to add salesMan and another member function to iterate through all the salesmen and print the details.
class SalesMen
{
public:
SalesMen() = default;
public:
void AddSalesMan(const Salesman& salesMan)
{
salesMen.push_back(salesMan);
}
void PrintDetails()
{
for (auto& salesMan : salesMen)
{
salesMan.displaySalesmanDetails();
}
}
private:
std::vector<Salesman> salesMen;
};
int main(int argc, char* argv[])
{
SalesMen salesMen{};
Salesman s1;
s1.setSalesmanDetails( 1, "John", 30000, "772358375" );
salesMen.AddSalesMan(s1);
Salesman s2;
s2.setSalesmanDetails(2, "Ann", 40000, "773029452");
salesMen.AddSalesMan(s2);
Salesman s3;
s3.setSalesmanDetails(3, "Leema", 35000, "778294526");
salesMen.AddSalesMan(s3);
salesMen.PrintDetails();
}
For the output of a class, you need to overwrite the insertion operator <<. In this, you do nearly the same as in your display function. With that, you can output your code to std::cout or a file or any other stream.
Please read about operator overloading and especially the inserter and the extractor.
And in your main function, you do can simply use a std::vector to store your salesman.
And then output verything in a simple loop using the above insertion operator.
Please see the below example code:
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using std::string;
using std::cout;
using std::endl;
class Salesman
{
private:
int salesmanId;
string salesmanName;
int salary;
string contactNumber;
public:
Salesman();
~Salesman();
void setSalesmanDetails(int id, string name, int sal, string number);
void displaySalesmanDetails();
void setSalesmanContactNumber(string number);
friend std::ostream& operator << (std::ostream& os, const Salesman& sm) {
return os << "Salesman ID: " << sm.salesmanId << endl
<< "Salesman Name: " << sm.salesmanName << endl
<< "Salary: " << sm.salary << endl
<< "Contact Number: " << sm.contactNumber << endl << endl;
}
};
void Salesman ::setSalesmanDetails(int id, string name, int sal, string number)
{
salesmanId = id;
salesmanName = name;
salary = sal;
contactNumber = number;
}
void Salesman ::displaySalesmanDetails()
{
cout << "Salesman ID: " << salesmanId << endl;
cout << "Salesman Name: " << salesmanName << endl;
cout << "Salary: " << salary << endl;
cout << "Contact Number: " << contactNumber << endl
<< endl;
}
void Salesman::setSalesmanContactNumber(string number)
{
contactNumber = number;
}
Salesman::Salesman()
{
}
Salesman::~Salesman()
{
}
int main(int argc, char *argv[])
{
std::vector<Salesman> salesman{};
string number;
Salesman s1;
s1.setSalesmanDetails(1, "John", 30000, "772358375");
salesman.push_back(s1);
Salesman s2;
s2.setSalesmanDetails(2, "Ann", 40000, "773029452");
salesman.push_back(s2);
Salesman s3;
s3.setSalesmanDetails(3, "Leema", 35000, "778294526");
salesman.push_back(s3);
for (const Salesman& sm : salesman)
std::cout << sm;
}
Related
The code compiles it does not read staff.txt. Not sure how to fix in the code. the error must be in reading the staff.txt of the code. Does anyone have an idea? output is in the image provided. Background on the question:
You are tasked to create 2 ADTs: customers and staff. Each customer will have a first and last name, an indication if the customer is enrolled in the “preferred” program, the car they are driving, and the amount of money owed to the company. On the other hand, each staff member will have a first and last name, the car they are driving, and their salary. In a real-world problem, there would obviously be more records associated with each staff member and customer.
ODU rentals would like you to automatically process txt files and print the information to the console screen. When you perform this process, ensure that your member variables are private. In addition, be sure to use getter and setter functions to manipulate and print your data. You may print your data in any format as long as it is organized. An example of correct output can be seen below.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
class Customer {
string fname;
string lname;
int enroll;
string car;
float amount;
public:
//Defining the customer ADT
Customer();
void setFirstName(string fnm);
void setLastName(string lnm);
void setEnroll(int e);
void setCarName(string cnm);
void setAmount(float amt);
string getFirstName();
string getLastName();
int getEnroll();
string getCarName();
float getAmount();
void printData();
};
Customer::Customer() {
fname = lname = car = "";
enroll = 0;
amount = 0;
}
void Customer::setFirstName(string fnm) {
fname = fnm;
}
void Customer::setLastName(string lnm) {
lname = lnm;
}
void Customer::setEnroll(int e) {
enroll = e;
}
void Customer::setCarName(string cnm) {
car = cnm;
}
void Customer::setAmount(float amt) {
amount = amt;
}
string Customer::getFirstName() {
return(fname);
}
string Customer::getLastName() {
return(lname);
}
int Customer::getEnroll() {
return(enroll);
}
string Customer::getCarName() {
return(car);
}
float Customer::getAmount() {
return(amount);
}
void Customer:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left <<setw(15) << getEnroll();
cout << left << setw(15) << getCarName() << left << setw(15) << fixed << setprecision(2) << getAmount() << endl;
}
class Staff {
string fname;
string lname;
string car;
float salary;
public:
//Defining staff ADT
Staff();
void setFirstName(string fnm);
void setLastName(string lnm);
void setCarName(string cnm);
void setSalary(float sal);
string getFirstName();
string getLastName();
string getCarName();
float getSalary();
void printData();
};
Staff::Staff() {
fname = lname = car = "";
salary = 0;
}
void Staff::setFirstName(string fnm) {
fname = fnm;
}
void Staff:: setLastName(string lnm) {
lname = lnm;
}
void Staff::setCarName(string cnm) {
car = cnm;
}
void Staff::setSalary(float sal) {
salary = sal;
}
string Staff::getFirstName() {
return(fname);
}
string Staff::getLastName() {
return(lname);
}
string Staff::getCarName() {
return(car);
}
float Staff::getSalary() {
return(salary);
}
void Staff:: printData() {
cout << left << setw(25) << getFirstName()+" "+getLastName() << left << setw(15);
cout << getCarName() << left << setw(15) << fixed << setprecision(2) << getSalary() <<endl;
}
//Main function
//Read txt files
int main() {
string fnm, lnm, carnm;
float amt, sal;
int e, i=0, recno;
ifstream fstaff("staff.txt");
if(!fstaff) {
cout << "File can not be opened" << endl;
exit(1);
}
fstaff >> recno;
Staff s[recno];
cout << left<<setw(25) << "Name" << left << setw(15) << "Car" << left << setw(15) << "Salary" <<endl;
cout << "____________________________________________________________________________" << endl;
for(i=0; i<recno; i++) {
fstaff>>fnm>>lnm>>carnm>>sal;
s[i].setFirstName(fnm);
s[i].setLastName(lnm);
s[i].setCarName(carnm);
s[i].setSalary(sal);
s[i].printData();
}
fstaff.close();
ifstream fcust("customers.txt");
if(!fcust) {
cout << "File can not be opened"<<endl;
exit(1);
}
fcust >> recno;
Customer c[recno];
cout << endl << endl;
cout << left << setw(25) << "Name" << left << setw(15) << "Preferred" << left << setw(15) << "Car" << left << setw(15) << "Bill" << endl;
cout <<"____________________________________________________________________________"<<endl;
for(i=0; i<recno; i++) {
fcust >> fnm >> lnm >> e >> carnm >>amt;
c[i].setFirstName(fnm);
c[i].setLastName(lnm);
c[i].setEnroll(e);
c[i].setCarName(carnm);
c[i].setAmount(amt);
c[i].printData();
}
fcust.close();
return 1;
}
Code Output
This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Closed 2 years ago.
I created a Class Student with instance data members
like studentname, studentaddress, studentrollno
i declared private static data member CollegeName that could be shared among all instances of class.
used promptfunction to take user input from the console
next functions to display student details.
Meanwhile resulting in below errors :
Meanwhile codes goes here:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int studentrollno;
string studentName;
string studentAddress;
static string CollegeName;
public:
void setRollNo(int rollno) {
studentrollno = rollno;
}
int getRollNo() {
return studentrollno;
}
void setName(string name) {
studentName = name;
}
string getName() {
return studentName;
}
void setAddress(string address) {
studentAddress = address;
}
string getAddress() {
return studentAddress;
}
static void setCollegeName(string collegename) {
CollegeName = collegename;
}
static string getCollegeName() {
return CollegeName;
}
void displayStudentDetails(); // member functions declare inside class Meanwhile it is defined
outside
static void show() {
cout << "this is static function " << endl;
}
};
// member functions define outside Student class
void Student :: displayStudentDetails() {
cout << "Student Name : " << getName() << " Student RollNo : " << getRollNo() << "Student Address
: " << getAddress() << "CollegeName: "<< getCollegeName() << endl;
}
void promptValues(Student &student) {
cout << "Enter the student Details " << endl;
cout << "Enter the details about student objects " << endl;
cout << endl;
int studentrollno;
string studentname , studentaddress ;
string collegename;
cout << "Enter the RollNo of the student " << endl;
cin >> studentrollno;
cout << "Enter the Name of the student " << endl;
cin >> studentname;
cout << endl;
cout << "Enter the address of the student " << endl;
cin >> studentaddress;
cout << endl;
cout << "Enter the collegeName of the student " << endl;
cin >> collegename;
student.setRollNo(studentrollno), student.setName(studentname), student.setAddress(studentaddress),
student.setCollegeName(collegename);
}
int main() {
Student student1, student2 , student3 , student4 , student5 ;
Student studentarrays[5] = { student1, student2, student3, student4, student5 };
Student studentmodel[5];
for (int i = 0; i < 5; i++) {
Student model;
model = studentarrays[i];
promptValues(model);
studentmodel[i] = model;
}
for (Student studentdetails : studentmodel) {
studentdetails.displayStudentDetails();
}
Student::show();
return 0;
}
You have omited the definition of the static member Student::CollegeName. It is only declared in the class, now you should define it after the class declaration with this:
std::string Student::CollegeName;
For more information see https://en.cppreference.com/w/cpp/language/static
As the title suggest, I am trying to sort a vector of objects by the GPA. My main issue is putting a method where I define the function for my comparison. The vector itself holds objects of five fields, which includes: string name, string ssn, string year, float credit, float gpa. I know theirs a way to use the sort operator. I am new to C++ so I am not very knowledgeable in this language. I appreciate the help! Here is the code:
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <list>
using namespace std;
class Student {
//declare local variables
protected:
string name; //people with names longer than 21 characters will just
have to make do
string ssn; // Social Secturity Number.
float gpa; //Most up to date gpa for the student
float credits; //Number of student's credit hours
//build public methods
public:
//Default Constructor
Student() {}
//Student constructor. Besides the character arrays, everything else is passed by reference.
Student(const string n, const string s, string sGPA, string sCredits) {
name = n;
ssn = s;
gpa = (float)atof(sGPA.c_str());
credits = (float)atof(sCredits.c_str());
}
string getName() {
return name;
}
string getSSN() {
return ssn;
}
float getGPA() {
return gpa;
}
float getCredit() {
return credits;
}
//a function that is expected to be implemented and overridden by subclasses
virtual void print() const {
cout << '\n' << endl;
cout << "Student's name: " << name << endl;
cout << "Student SSN: " << ssn << endl;
cout << "Student's current GPA: " << gpa << endl;
cout << "Student's credit hours: " << credits << endl;
}
// a pure virtual function for implementation later. Makes whole class Abstract
virtual float tuition() const = 0;
};
class Undergrad : public Student {
//declare local variables
protected:
float undergrad_rate = 380.0;
string year;
//build public methods
public:
//Default Constructor
Undergrad() {}
//Undergrad Constructor
Undergrad(const string n, const string s, string uGPA, string uCredits, string y) :
Student(n, s, uGPA, uCredits), year(y) {}
//Display the contents of undergrad
void print() const {
Student::print();
cout << "Undergrad Rate: " << undergrad_rate << endl;
cout << "Year: " << year << endl;
}
//Display undergrad's current year
string get_year() {
return year;
}
//Display the undergrad's current rate
float get_rate() {
return undergrad_rate;
}
//Set a undergrad's current year
void set_year(string y) {
year = y;
}
//Display the cost for an undergrad to attend university
float tuition() const {
return 1000000;
}
};
int main() {
ifstream ip("data.txt");
if (!ip.is_open()) std::cout << "ERROR: File not found" << '/n';
string name;
string ssn;
string year;
string credit;
string gpa;
list<Undergrad> myList;
list<Undergrad>::iterator i;
//Undergrad g(name, ssn, year, credit, gpa);
while (ip.good()) {
getline(ip, name, ',');
getline(ip, ssn, ',');
getline(ip, gpa, ',');
getline(ip, credit, ',');
getline(ip, year, '\n');
// float number = stoi(gpa);
//float number1 = stoi(credit);
Undergrad g(name, ssn, year, credit, gpa);
myList.push_back(g);
}
ip.close();
//This deletes the last object in the list and stores it in a temp object. It assigns that object to the beginning of the list.
Undergrad temp = myList.back();
myList.pop_back();
myList.insert(myList.begin(), temp);
/* for (int i = 0; i < myList.size(); i++) {
cout << "Name: " << myList[i].getName() << endl;
cout << "SSN: " << myList[i].getSSN() << endl;
cout << "Year: " << file[i].get_year() << endl;
cout << "Credit: " << file[i].getCredit() << endl;
cout << "GPA " << file[i].getGPA() << endl;
cout << " " << endl;
}
*/
/*for (Undergrad &x : myList) { //Goes through my list and displays its contents
x.print(); //This isn't bringing up errors.
}
*/
//This code copy the contents of the list to a vector.
std::vector<Undergrad> vect{ std::make_move_iterator(std::begin(myList)),
std::make_move_iterator(std::end(myList)) };
std::sort(vect.begin(), vect.end(), CompareGPA);
for (Undergrad &x : vect) {
x.print();
}
system("pause");
return 0;
}
Furthermore this is the code Im trying to implement to get the comparision
bool CompareGPA(const Student& left, const Student& right) {
return left.gpa > right.gpa;
}
Using lambda you can implement like this:Use a property int get_gpa() const to access the protected member.
std::sort(vect.begin(), vect.end(), [] (const Student& left, const Student& right)
{
return left.get_gpa() > right.get_gpa();
});
Here how to implement the property ( a member function to return the protected variable) :
int get_gpa() const
{
return gpa;
}
This coding where I used function overloading is a code where user have to input their pointer for 4 subjects and its credit hour so it prints out their GPA. The thing is I have 3 parameter in Student(string test123, string nama, string vinto ). But I only want to display either one of the string. Lets say I want the Vinto to be print out. How can I call the vinto in my Display function so that it prints out Vinto. Heres's My coding.
CPP.cpp
#include <iostream>
#include "student.h"
using namespace std;
void main(void)
{
string nama;
string test123;
int i;
Student StudentA(test123, nama, "vinto");
cout << "key in points for 4 subject\n";
StudentA.Getpointers();
StudentA.Display(test123);
}
Student.h
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string test123, string nama, string vinto );
void Getpointers();
void Display(string name);
private:
double points[4];
int creditHours[4];
string name;
double CalculateGPA();
};
Student.cpp
#include <iostream>
#include <iomanip>
#include<string>
#include "student.h"
using namespace std;
Student::Student(string test123, string nama, string vinto)
{
name = nama;
}
void Student::Getpointers()
{
for (int i = 0; i<4; i++)
{
cout << "points for subject :" << i + 1 << endl;
cin >> points[i];
cout << "credit hour for subject " << i + 1 << endl;
cin >> creditHours[i];
}
}
void Student::Display(string name)
{
cout << "Hello " << name << endl;
cout << "Your current GPA is " << setprecision(3) << CalculateGPA() << endl;
}
double Student::CalculateGPA()
{
double totalpoints = 0;
int totalcreditHours = 0;
for (int i = 0; i<4; i++)
{
totalpoints += (points[i] * creditHours[i]);
totalcreditHours += creditHours[i];
}
return totalpoints / totalcreditHours;
}
Well, the vinto argument of your constructor is not saved anywhere, so in this example you cannot get it back. However, you could store it:
First, add a vinto field to the class:
class Student
{
public:
Student(string test123, string nama, string vinto );
void Getpointers();
void Display(string name);
private:
double points[4];
int creditHours[4];
string name;
string vinto;
double CalculateGPA();
};
Then, store the vinto argument value in this field:
Student::Student(string test123, string nama, string vinto)
{
name = nama;
this->vinto = vinto;
}
After this, you may use vinto:
void Student::Display(string name)
{
cout << "Hello " << name << endl;
cout << "Your current GPA is " << setprecision(3) << CalculateGPA() << endl;
cout << "Your vinto is " << vinto << endl;
}
Also, it's a bit strange that you store student's name in the object field, but use another name (which is passed to Student::Display) to say Hello to him.
When i run this program it get fatal error and program got stopped. I think this problem is due to string type of may be using std::string. because when i use char data type it was runs correctly. What is the problem kindly guide.
parcel.cpp
#include "Parcel.h"
#include <iostream>
#include <string>
using namespace std;
bool check = false;
// Constructor
Parcel::Parcel(int id, std::string senderName, std::string senderAddress, std::string receiverName, std::string receiverAddress, int weight, int fee){
this->id = id;
this->senderName = senderName;
this->senderAddress = senderAddress;
this->receiverName = receiverName;
this->receiverAddress = receiverAddress;
this->weight = weight;
this->fee = fee;
};
// Destructor
Parcel::~Parcel() {
cout << "Destructor called";
}
// Defination of setter
void Parcel::setID(int id) {
this->id = id;
if(id < 0) {
cout << endl << "Error: Please write valid receipt number e.g. 0 to onward";
}
}
void Parcel::setWeight(int weight) {
this->weight = weight;
if(weight < 0) {
cout << endl << "Error: Please write valid weight e.g. above to 0 grams";
}
}
void Parcel::setFee(int fee) {
this->fee = fee;
if(fee < 0) {
cout << endl << "Error: Please write valid fee e.g. 0 to onward";
}
}
void Parcel::setSenderName(std::string senderName) {
this->senderName = senderName;
// if(strlen(senderName) == 0) {
// cout << endl << "Error: Please write sender name";
// }
}
void Parcel::setSenderAddress(std::string senderAddress) {
this->senderAddress = senderAddress;
// if(senderAddress == 0) {
// cout << endl << "Error: Please write sender address";
// }
}
void Parcel::setReceiverName(std::string receiverName) {
this->receiverName = receiverName;
// if(receiverName == 0) {
// cout << endl << "Error: Please write reciever name";
// }
}
void Parcel::setReceiverAddress(std::string receiverAddress) {
this->receiverAddress = receiverAddress;
// if(receiverAddress == 0) {
// cout << endl << "Error: Please write reciever address";
// }
}
// Defination of getter
int Parcel::getID() {
return id;
}
int Parcel::getWeight() {
return weight;
}
int Parcel::getFee() {
return fee;
}
string Parcel::getSenderName() {
return senderName;
}
string Parcel::getSenderAddress() {
return senderAddress;
}
string Parcel::getReceiverName() {
return receiverName;
}
string Parcel::getReceiverAddress() {
return receiverAddress;
}
parcel.h
#ifndef PARCEL_H
#define PARCEL_H
#include <iostream>
#include <string>
class Parcel
{
private:
// Declare data members
int id, weight, fee;
std::string senderName, senderAddress, receiverName, receiverAddress;
public:
// Setter function
void setID(int id);
void setWeight(int weight);
void setFee(int fee);
void setSenderName(std::string senderName);
void setSenderAddress(std::string senderAddress);
void setReceiverName(std::string receiverName);
void setReceiverAddress(std::string receiverAddress);
// getter function
int getID();
int getWeight();
int getFee();
std::string getSenderName();
std::string getSenderAddress();
std::string getReceiverName();
std::string getReceiverAddress();
// Constructor
Parcel(int id, std::string senderName, std::string senderAddress, std::string receiverName, std::string receiverAddress, int weight, int fee);
// Destructor
~Parcel();
protected:
};
#endif
main.cpp
#include <iostream>
#include "Parcel.h"
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
// Data member to take input from user
int id, weight, fee;
std::string senderName, senderAddress, receiverName, receiverAddress;
// main function
int main(int argc, char** argv) {
cout << "Please enter receipt number: ";
cin >> id;
cout << "Please enter sender name: ";
cin >> senderName;
cout << "Please enter sender address: ";
cin >> senderAddress;
cout << "Please enter receiver name: ";
cin >> receiverName;
cout << "Please enter receiver address: ";
cin >> receiverAddress;
cout << "Please enter parcel weight(gms): ";
cin >> weight;
cout << "Please enter parcel fee: ";
cin >> fee;
// Instance of Parcel class
Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());
// Set values
parcel.setID(id);
parcel.setSenderName(senderName);
parcel.setSenderAddress(senderAddress);
parcel.setReceiverName(receiverName);
parcel.setReceiverAddress(receiverAddress);
parcel.setWeight(weight);
parcel.setFee(fee);
// Make output to show on console
cout << endl << endl;
cout << "Shipment Receipt" << endl << "___________________________________________" << endl;
cout << "Receipt No.: " << parcel.getID() << endl << "Sender Name: " << parcel.getSenderName() << endl
<< "Sender Address: " << parcel.getSenderAddress() << endl << "Receiver Name: " << parcel.getReceiverName() << endl
<< "Receiver Address: " << parcel.getReceiverAddress() << endl << "Parcel Weight: " << parcel.getWeight() << endl
<< "Parcel Shipping Charges: " << parcel.getFee() << endl;
// Destructor call
parcel.~Parcel();
}
Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());
Don't do that. You are calling parcel.getSenderName() before the parcel object is fully initialized.
Your program doesn't terminate with instance of bad alloc when I tried to run. However, it does get warnings because of this line
Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());
You should consider a default constructor Parcel() with default values assigned to different attributes and then Parcel parcel before assigning them in main()
What arguments did you run your program with?
Your construction,
Parcel parcel(parcel.getID(), parcel.getSenderName(), parcel.getSenderAddress(), parcel.getReceiverName(), parcel.getReceiverAddress(), parcel.getWeight(), parcel.getFee());
initialises parcel with itself, which is uninitialised.
This is undefined, and anything can happen.
You've already read all parameters from the user, so use them:
Parcel parcel(id, senderName, senderAddress, receiverName, receiverAddress, weight, fee);
You can remove the entire sequence of setters.
And you should not call the destructor; C++ handles destruction automatically.
If you found that idea in a book, throw it away immediately and look for a better one here.
If you don't have a book, look in the same place.