SlideShare uma empresa Scribd logo
1 de 22
cpphomeworkhelp.com
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- info@cpphomeworkhelp.com or
reach us at :- https://www.cpphomeworkhelp.com/
Problems
Problem 1.
Explain the output of the following code without running it:
struct data
{
float z;
char type;
};
int main()
{
data D1 = {20, 'P’};
cout << D1.z++ << D1.type << endl;
data D2;
D2.type = D1.type;
D2.z = 4 * D1.z;
Cout << ++D2.z << D2.type << endl;
Cout << D1.z-- <<D1.type;
return 0;
}
cpphomeworkhelp.com
Problem 2.
Debug the following program. Include the entire fully functional program as
your answer.
class member
{ int memberNum = 25;
float memberPay;
public
void Input(cin >> memberNum >> memberPay);
void Output;
}
int main()
{ Member mem;
cin >> mem.memberNum >> mem.memberPay;
cout << mem.memberNum << 't' << mem.memberPay;
}
void Output::Member()
{
cout << mem.memberNum << 't' << mem.memberPay;
}
cpphomeworkhelp.com
Problem 3.
1. Define a class Account (to indicate bank accounts) with the following
specifications:
a. Data members in private visibility mode (remember that the default
visibility mode is private):
i. Account number (long integer)
ii. Name of the depositor (string object)
iii. Type of account (character type value which stores either ‘S’ for saving and
‘C’ for checking)
iv. Balance Amount (double)
b. Member functions in public:
v. To input the value of all data members based on values inputted by the
user.
vi. To deposit money (the function should take an argument called
amountToDeposit, increment the current balance amount by this value, and
return the new balance)
cpphomeworkhelp.com
vii. To withdraw money from the account. It should do what the previous
case did with a similar argument named amountToWithdraw. However, before
doing the withdrawal, it should make sure that the balance after withdrawing
will not drop below $500. It should return the amount actually withdrawn (0 if
no money was withdrawn).
viii. To return the Balance Amount
ix. To print all the data members.
2. Write a function which takes two Account objects as arguments, and sets the
data members of these objects based on values inputted by the user. The object
in the first argument should retain these values after the function is called, but
the object in the second argument should not. Remember that since this
function will not be a member of the class, it only has public access to the class
members.
3. Write another function which again takes two objects as arguments,
compares their balance amount (using the function you defined in question
1b.viii), and returns that object whose balance amount is greater. Define the
function such that modifying the Account object returned will modify the
original object passed as an argument.
cpphomeworkhelp.com
Problem 4.
Classes can also have arrays data members inside them. For instance, if we
have an array A of size 3 as a data member of the class Employee we discussed
about in lecture, an object of the class Emp1 will have 3 sub-variables, namely
Emp1.A[0], Emp2.A[1], Emp3.A[2], similar to the regular sub-variables
Emp1.Id, Emp1.Name etc.
Define a class SchoolStudent with the following specifications. You may assume
every student has 8 grades.
1. Data members in private visibility mode
a. studentID, of type long integer
b. studentName, of type string
c. average, of type float
d. grades, an array of type double
2. Member function in private visibility mode. (If a member function is defined
in private visibility mode, then it can only be accessed by other member
functions inside the class but not by the objects of the class).
cpphomeworkhelp.com
a. CalculateAvg() that returns the average of the students’ grades.
3. Member functions in public:
a. Assign() that asks the user to input the ID, Name and Grades and sets the
average equal to the value returned after calling the Calculate() function.
b. Output() that outputs all of the data on to the screen
Problem 5.
An array of class objects works the same way as a normal array. For example,
if we declare an array of objects of the class Employee discussed in lecture:
Employee Emp[10];
Employee Emp[10]; Each element of the array will be an object, i.e Emp[0],
Emp[1], and Emp[2] are all Employee objects (or instances of the Employee
class). If we wish to call the Input() function for object Emp[1], the syntax will
be just as in the case of a normal object: Emp[1].Input();.
In classes, data members are usually made private. If you want to allow
outside users of the class to see or modify these values, the class usually
should make sure this happens on the class’s own terms – it doesn’t want any
cpphomeworkhelp.com
other code making its internal data inconsistent (say, a SchoolStudent’s
average being set to a number not reflective of the grades). To selectively allow
access, classes often have “getter” and “setter” functions. For example, to allow
users of the Employee class to retrieve and modify the employeeId, we might
define:
class Employee {
…
int getId() { return Id; }
void setId(int newId) { Id = newId; }
…
};
The names of these functions are usually of the form getSomeVariable or
setSomeVariable. In the class defined in the previous question, add a getter
function in public visibility mode to return the percentage of a student. Write a
program to declare an array of objects of class SchoolStudent and perform the
following using separate global functions (i.e. non-member functions) for each.
(Use dynamic memory allocation to allow the user to decide how many
students are in the array.)
cpphomeworkhelp.com
1. Assigning values to all the elements of the array (Calling the Assign()
function for each of the objects)
2. Sorting the array of class objects on the basis of average using bubble sort
method (you will have to call your average getter in the if-statement of the
sort)
3. Displaying the array elements (You will have to call the Output() function
for each object)
Problem 6.
1. Write a Rectangle class that has as data members two arrays of doubles,
one for x coordinates and the other for y coordinates. The class should
have an init() function that inputs these coordinates from the user. (We
will see a more elegant way to initialize in the next lecture.)
2. Write a RectDrawing class that has as a data member an array of
Rectangles. Create an init() function that allocates a new array of
Rectangles of a size entered by the user, and allows the user to initialize
each Rectangle.
What problem will occur if a RectDrawing is initialized more than once?
Assuming you had some well-implemented hasBeenInitialized() member
cpphomeworkhelp.com
cpphomeworkhelp.com
function, and you were adding the code below to the init function of
RectDrawing, what would you have to put in the blank line below to solve
this problem?
if( hasBeenInitialized() ) {
// Insert your code here
}
4. Show, in one line, how you would allocate a new RectDrawing class using
the new operator (assuming you wanted to be able to refer to the new object
later).
Solution 1.
1. The program first defines a structure data containing two variables z (of type
float), and type (of datatype char).
2. In the main function, a structure type variable of the structure data is declared,
and then initialized (D1.z is initialized to 20, and D1.type is initialized to P)
3. D1.z++ increments D1.z to 21, but since the increment is done by the
postincrement operator, the cout prints 20, followed by ‘P’ (value stored in
D1.type), on the screen.
4. A new structure type variable of structure data is declared. The value of D2.type
is initialized to the value stored in D1.type (i.e P). The value of D2.z is initialized
to 4 times the value stored in D1.z( i.e 21*4=84).
5. ++D2.z increments the value of D2.z by 1 (i.e to 85), and as the increment is
done by the pre-increment operator, cout print 85 is on the screen, followed by
‘P’ (value stored in D2.type).
6. Finally, in the next line, D1.z is decremented by 1, but due to post-increment, its
previous value (i.e 20) is printed to the screen by the cout, followed by printing
of the character ‘P’ (D1.type).
Output:
20P
85P
21P
cpphomeworkhelp.com
Solutions
Solution 2.
class member
{
int memberNum;
float memberPay;
public:
void Input(){cin >> memberNum >> memberPay};
void Output();
};
int main()
{
Member mem;
mem.Input();
mem.Output();
return 0;
}
void Member::Output()
{
cout << memberNum << 't' << memberPay;
}
cpphomeworkhelp.com
Solution 3.
class Account
{
long AccNum;
string Depositor;
char AccType;
double Amount;
public:
void Input();
double Deposit( double amountToDeposit);
double Withdraw (double amountToWithdraw);
double retBalance();
void Output();
}
void Account::Input()
{
cin >> AccNum >> Depositor >> AccType >> Amount;
}
void Account::Output()
{
cpphomeworkhelp.com
cout << AccNum << ":" << Depositor << ":"
<< AccType << ":" << Amount << endl;
}
double Account::Deposit (double amountToDeposit)
{
Balance += amountToDeposit;
return Balance;
}
double Account::Withdraw (double amountToWithdraw)
{
if(Balance-amountToWithdraw >= 500)
{
Balance -= amountToWithdraw;
return Balance;
}
else
{
cerr << "Not allowed to withdraw" << endl;
return 0;
}
}
cpphomeworkhelp.com
double Account::retBalance()
{
return Balance;
void Input(Account &Acc1, Account Acc2)
{
Acc1.Input();
Acc2.Input();
}
Account CompareBal(Account &Acc1, Account &Acc2)
{
if(Acc1.retBalance()>Acc2.retBalance())
return Acc1;
else
return Acc2;
}
cpphomeworkhelp.com
Solution 4.
class SchoolStudent
{
private: // Optional
long studentID;
string studentName;
float average;
double grades[8];
float CalculateAvg();
public:
void Assign();
void Output();
};
void SchoolStudent::Assign()
{
cin >> studentID >> studentName;
for(int i=0;i> grades[i];
average = CalculateAvg();
}
cpphomeworkhelp.com
cpphomeworkhelp.com
float SchoolStudent::CalculateAvg()
{
float sum=0;
for(int i =0; i < 8; i++)
sum += grades[i];
return sum/8.0;
}
void SchoolStudent::Output()
{
cout << studentID << ":" << studentName << ":"
<< average << ":";
for(int i = 0; i < 8; i++)
cout << grades[i];
}
Solution 5.
This question uses the same class as in the previous question, with an
additional member function in public, i.e. GetAvg().
float SchoolStudent::GetAvg()
{
return average;
}
void Assign( SchoolStudent *Student, int n)
{
for(int i=0;
Student[i].Assign();
}
void Sort (SchoolStudent *Student, int n)
{
for(int i=0;I <5;i++)
{
for( int j=0;j<i;j++)
{
if(Student[i].GetAvg() > Student[j].GetAvg())
cpphomeworkhelp.com
{
Student temp=Student[i]; //swap
Student[i]=Student[j];
Student[j]=temp;
}
}
}
}
void Display(SchoolStudent *Student, int
n)
{
for(int i = 0; i < n; i++)
Student[i].Output();
}
int main()
{
int n;
cout << "Enter the number of students:";
cin >> n;
SchoolStudent *Student1 = new
cpphomeworkhelp.com
cpphomeworkhelp.com
SchoolStudent[n];
Assign(Student1, n);
Sort(Student1, n);
Display(Student1, n);
}
Solution 6.
class Rectangle
{
double x[4], y[4];
public:
void init();
};
Rectangle::init()
{
for (int i = 0; i < 4; i++)
cin >> x[i] >> y[i];
}
class RectDrawing
{
Rectangle *ArrRect;
public:
void init();
};
void Rectdrawing::init()
cpphomeworkhelp.com
{
int n;
cin >> n;
ArrRect = new Rectangle[n];
}
If we keep initializing without deleting, then each previous array will be lost,
but will continue to sit in memory. More and more memory will keep being
allocated to the arrays, and eventually the program will run out of memory.
void RectDrawing::init()
{
if( hasBeenInitialized() )
delete[] ArrRect;
int n;
cin >> n;
Rectangle *ArrRect= new Rectangle[n];
}
RectDrawing *Arr=new RectDrawing;
cpphomeworkhelp.com

Mais conteúdo relacionado

Semelhante a CPP Homework Help

Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfadityastores21
 
Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materialsgayaramesh
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfstudy material
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objectsRai University
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxvrickens
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2Kaela Johnson
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 

Semelhante a CPP Homework Help (20)

Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materials
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 
Oops recap
Oops recapOops recap
Oops recap
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Ch 4
Ch 4Ch 4
Ch 4
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 

Mais de C++ Homework Help (19)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Último

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 

CPP Homework Help

  • 1. cpphomeworkhelp.com For any Homework related queries, Call us at : - +1 678 648 4277 You can mail us at :- info@cpphomeworkhelp.com or reach us at :- https://www.cpphomeworkhelp.com/
  • 2. Problems Problem 1. Explain the output of the following code without running it: struct data { float z; char type; }; int main() { data D1 = {20, 'P’}; cout << D1.z++ << D1.type << endl; data D2; D2.type = D1.type; D2.z = 4 * D1.z; Cout << ++D2.z << D2.type << endl; Cout << D1.z-- <<D1.type; return 0; } cpphomeworkhelp.com
  • 3. Problem 2. Debug the following program. Include the entire fully functional program as your answer. class member { int memberNum = 25; float memberPay; public void Input(cin >> memberNum >> memberPay); void Output; } int main() { Member mem; cin >> mem.memberNum >> mem.memberPay; cout << mem.memberNum << 't' << mem.memberPay; } void Output::Member() { cout << mem.memberNum << 't' << mem.memberPay; } cpphomeworkhelp.com
  • 4. Problem 3. 1. Define a class Account (to indicate bank accounts) with the following specifications: a. Data members in private visibility mode (remember that the default visibility mode is private): i. Account number (long integer) ii. Name of the depositor (string object) iii. Type of account (character type value which stores either ‘S’ for saving and ‘C’ for checking) iv. Balance Amount (double) b. Member functions in public: v. To input the value of all data members based on values inputted by the user. vi. To deposit money (the function should take an argument called amountToDeposit, increment the current balance amount by this value, and return the new balance) cpphomeworkhelp.com
  • 5. vii. To withdraw money from the account. It should do what the previous case did with a similar argument named amountToWithdraw. However, before doing the withdrawal, it should make sure that the balance after withdrawing will not drop below $500. It should return the amount actually withdrawn (0 if no money was withdrawn). viii. To return the Balance Amount ix. To print all the data members. 2. Write a function which takes two Account objects as arguments, and sets the data members of these objects based on values inputted by the user. The object in the first argument should retain these values after the function is called, but the object in the second argument should not. Remember that since this function will not be a member of the class, it only has public access to the class members. 3. Write another function which again takes two objects as arguments, compares their balance amount (using the function you defined in question 1b.viii), and returns that object whose balance amount is greater. Define the function such that modifying the Account object returned will modify the original object passed as an argument. cpphomeworkhelp.com
  • 6. Problem 4. Classes can also have arrays data members inside them. For instance, if we have an array A of size 3 as a data member of the class Employee we discussed about in lecture, an object of the class Emp1 will have 3 sub-variables, namely Emp1.A[0], Emp2.A[1], Emp3.A[2], similar to the regular sub-variables Emp1.Id, Emp1.Name etc. Define a class SchoolStudent with the following specifications. You may assume every student has 8 grades. 1. Data members in private visibility mode a. studentID, of type long integer b. studentName, of type string c. average, of type float d. grades, an array of type double 2. Member function in private visibility mode. (If a member function is defined in private visibility mode, then it can only be accessed by other member functions inside the class but not by the objects of the class). cpphomeworkhelp.com
  • 7. a. CalculateAvg() that returns the average of the students’ grades. 3. Member functions in public: a. Assign() that asks the user to input the ID, Name and Grades and sets the average equal to the value returned after calling the Calculate() function. b. Output() that outputs all of the data on to the screen Problem 5. An array of class objects works the same way as a normal array. For example, if we declare an array of objects of the class Employee discussed in lecture: Employee Emp[10]; Employee Emp[10]; Each element of the array will be an object, i.e Emp[0], Emp[1], and Emp[2] are all Employee objects (or instances of the Employee class). If we wish to call the Input() function for object Emp[1], the syntax will be just as in the case of a normal object: Emp[1].Input();. In classes, data members are usually made private. If you want to allow outside users of the class to see or modify these values, the class usually should make sure this happens on the class’s own terms – it doesn’t want any cpphomeworkhelp.com
  • 8. other code making its internal data inconsistent (say, a SchoolStudent’s average being set to a number not reflective of the grades). To selectively allow access, classes often have “getter” and “setter” functions. For example, to allow users of the Employee class to retrieve and modify the employeeId, we might define: class Employee { … int getId() { return Id; } void setId(int newId) { Id = newId; } … }; The names of these functions are usually of the form getSomeVariable or setSomeVariable. In the class defined in the previous question, add a getter function in public visibility mode to return the percentage of a student. Write a program to declare an array of objects of class SchoolStudent and perform the following using separate global functions (i.e. non-member functions) for each. (Use dynamic memory allocation to allow the user to decide how many students are in the array.) cpphomeworkhelp.com
  • 9. 1. Assigning values to all the elements of the array (Calling the Assign() function for each of the objects) 2. Sorting the array of class objects on the basis of average using bubble sort method (you will have to call your average getter in the if-statement of the sort) 3. Displaying the array elements (You will have to call the Output() function for each object) Problem 6. 1. Write a Rectangle class that has as data members two arrays of doubles, one for x coordinates and the other for y coordinates. The class should have an init() function that inputs these coordinates from the user. (We will see a more elegant way to initialize in the next lecture.) 2. Write a RectDrawing class that has as a data member an array of Rectangles. Create an init() function that allocates a new array of Rectangles of a size entered by the user, and allows the user to initialize each Rectangle. What problem will occur if a RectDrawing is initialized more than once? Assuming you had some well-implemented hasBeenInitialized() member cpphomeworkhelp.com
  • 10. cpphomeworkhelp.com function, and you were adding the code below to the init function of RectDrawing, what would you have to put in the blank line below to solve this problem? if( hasBeenInitialized() ) { // Insert your code here } 4. Show, in one line, how you would allocate a new RectDrawing class using the new operator (assuming you wanted to be able to refer to the new object later).
  • 11. Solution 1. 1. The program first defines a structure data containing two variables z (of type float), and type (of datatype char). 2. In the main function, a structure type variable of the structure data is declared, and then initialized (D1.z is initialized to 20, and D1.type is initialized to P) 3. D1.z++ increments D1.z to 21, but since the increment is done by the postincrement operator, the cout prints 20, followed by ‘P’ (value stored in D1.type), on the screen. 4. A new structure type variable of structure data is declared. The value of D2.type is initialized to the value stored in D1.type (i.e P). The value of D2.z is initialized to 4 times the value stored in D1.z( i.e 21*4=84). 5. ++D2.z increments the value of D2.z by 1 (i.e to 85), and as the increment is done by the pre-increment operator, cout print 85 is on the screen, followed by ‘P’ (value stored in D2.type). 6. Finally, in the next line, D1.z is decremented by 1, but due to post-increment, its previous value (i.e 20) is printed to the screen by the cout, followed by printing of the character ‘P’ (D1.type). Output: 20P 85P 21P cpphomeworkhelp.com Solutions
  • 12. Solution 2. class member { int memberNum; float memberPay; public: void Input(){cin >> memberNum >> memberPay}; void Output(); }; int main() { Member mem; mem.Input(); mem.Output(); return 0; } void Member::Output() { cout << memberNum << 't' << memberPay; } cpphomeworkhelp.com
  • 13. Solution 3. class Account { long AccNum; string Depositor; char AccType; double Amount; public: void Input(); double Deposit( double amountToDeposit); double Withdraw (double amountToWithdraw); double retBalance(); void Output(); } void Account::Input() { cin >> AccNum >> Depositor >> AccType >> Amount; } void Account::Output() { cpphomeworkhelp.com
  • 14. cout << AccNum << ":" << Depositor << ":" << AccType << ":" << Amount << endl; } double Account::Deposit (double amountToDeposit) { Balance += amountToDeposit; return Balance; } double Account::Withdraw (double amountToWithdraw) { if(Balance-amountToWithdraw >= 500) { Balance -= amountToWithdraw; return Balance; } else { cerr << "Not allowed to withdraw" << endl; return 0; } } cpphomeworkhelp.com
  • 15. double Account::retBalance() { return Balance; void Input(Account &Acc1, Account Acc2) { Acc1.Input(); Acc2.Input(); } Account CompareBal(Account &Acc1, Account &Acc2) { if(Acc1.retBalance()>Acc2.retBalance()) return Acc1; else return Acc2; } cpphomeworkhelp.com
  • 16. Solution 4. class SchoolStudent { private: // Optional long studentID; string studentName; float average; double grades[8]; float CalculateAvg(); public: void Assign(); void Output(); }; void SchoolStudent::Assign() { cin >> studentID >> studentName; for(int i=0;i> grades[i]; average = CalculateAvg(); } cpphomeworkhelp.com
  • 17. cpphomeworkhelp.com float SchoolStudent::CalculateAvg() { float sum=0; for(int i =0; i < 8; i++) sum += grades[i]; return sum/8.0; } void SchoolStudent::Output() { cout << studentID << ":" << studentName << ":" << average << ":"; for(int i = 0; i < 8; i++) cout << grades[i]; }
  • 18. Solution 5. This question uses the same class as in the previous question, with an additional member function in public, i.e. GetAvg(). float SchoolStudent::GetAvg() { return average; } void Assign( SchoolStudent *Student, int n) { for(int i=0; Student[i].Assign(); } void Sort (SchoolStudent *Student, int n) { for(int i=0;I <5;i++) { for( int j=0;j<i;j++) { if(Student[i].GetAvg() > Student[j].GetAvg()) cpphomeworkhelp.com
  • 19. { Student temp=Student[i]; //swap Student[i]=Student[j]; Student[j]=temp; } } } } void Display(SchoolStudent *Student, int n) { for(int i = 0; i < n; i++) Student[i].Output(); } int main() { int n; cout << "Enter the number of students:"; cin >> n; SchoolStudent *Student1 = new cpphomeworkhelp.com
  • 21. Solution 6. class Rectangle { double x[4], y[4]; public: void init(); }; Rectangle::init() { for (int i = 0; i < 4; i++) cin >> x[i] >> y[i]; } class RectDrawing { Rectangle *ArrRect; public: void init(); }; void Rectdrawing::init() cpphomeworkhelp.com
  • 22. { int n; cin >> n; ArrRect = new Rectangle[n]; } If we keep initializing without deleting, then each previous array will be lost, but will continue to sit in memory. More and more memory will keep being allocated to the arrays, and eventually the program will run out of memory. void RectDrawing::init() { if( hasBeenInitialized() ) delete[] ArrRect; int n; cin >> n; Rectangle *ArrRect= new Rectangle[n]; } RectDrawing *Arr=new RectDrawing; cpphomeworkhelp.com