Online CPP Homework Help
Online CPP Homework Help
Problem
Initializer syntax:
class_name::class_name(argument_list) :
firstDataMemberToInitialize(initialValue1),
secondDataMemberToInitialize(initialValue
This initialization method – the colon after the constructor arguments,
followed by a comma-separated list of initializations to perform – can be
used for any base class constructor or data member. It can also be used
to call one constructor from another.
To give one more example of the use of this syntax, a colorable Square
class that inherits from a black- and-white Rectangle class might want to
define a constructor like this:
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
b. Rewrite the constructor to use the member initializer syntax instead
of an assignment statement.
Problem 2
Create a class called Complex for performing arithmetic with complex
numbers. Write a program to test your class.
Complex numbers have
the form realPart +
imaginaryPart * i
where i is1 .
Use double variables to represent the private data of the class. Provide a
constructor that enables an object of this class to be initialized when it
is declared. The constructor should contain default values in case no
initial values are provided (a reasonable default number to set a
complex number to is 0 0i ). Provide public member functions that
perform the following tasks:
a. Adding another Complex number: The real parts are added
together and the imaginary parts are added together.
b. Subtracting another Complex number: The real part of the right
operand is subtracted from the real part of the left operand, and
the imaginary part of the right operand is subtracted from the
imaginary part of the left operand.
c. Multiply by another Complex number: The product of the
imaginary parts is subtracted from the product of the real parts to
get the new real part, and the products of the imaginary and real
and real and imaginary parts are added to form the new imaginary
part.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
c.Print Complex numbers in the form (a, b), where a is the real part
and b is the imaginary part. The member functions should each return
another Complex object, and should be called with a syntax
like complex1.add(complex2);.
we could specify:
class Dog {
friend void Person::Walk();
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
…
};
Any static class member can be accessed from within a class function.
Additionally, any public class member can be accessed either via an
object of the class or via the class name; the two definitions of count
below are equivalent:
MartianInvader myMartian;
int count = myMartian.getMartianCount();
int count = MartianInvader::getMartianCount(); //Use scope resolution
operator (::)
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Constant Member Functions:
Find the errors in the following class and explain how to correct
them.
class
StockPurchase {
public:
StockPurch
ase(const
double
newPrice) {
//
When
someo
For any Homework related queries, Call us at : - +1 678 648 4277
ne You can mail us at : - [email protected] or
buys a reach us at : - https://www.cpphomeworkhelp.com/
previousDefaultPrice = defaultPrice;
currentPrice = newPrice;
defaultPrice =
previousDefaultPrice
+ (currentPrice - previousDefaultPrice) / 2.0;
timeLastChecked = time(0); // record the current
timestamp
}
private:
double previousDefaultPrice,
currentPrice; static double defaultPrice;
time_t timeLastChecked; // A time_t is
basically just an integer
}
Problem 4
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Fill in the blanks in each of the following statements (you may wish to
refer to the chart on Page 15 of the lecture notes):
Problem 5
Create an inheritance hierarchy containing base class Account and
derived classes SavingsAccount and CheckingAccount that inherit
from class Account. Make sure all return values, arguments, and
member functions are declared const where appropriate.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
a. Base class Account should include one data member of type double to
represent the account balance. The class should provide a constructor
that receives an initial balance and uses it to initialize the data
member. The constructor should validate the initial balance to ensure
that it is greater than or equal to 0.0. If not, the balance should be set
to 0.0 and the constructor should display an error message, indicating
that the initial balance was invalid. (You may wish to use the cerr
object, which functions just like cout except that it outputs to the
standard error output rather than standard text output. Usually, but not
always, the two are functionally similar.)
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
formula I B * (1 r)t , where I is the interest, B is the initial
balance, r is the interest rate, and t is the number of years gone by.
[Note: SavingsAccount should inherit member functions credit and
debit as is without redefining them.]
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
the function definitions in a SomeClass.cpp file (which includes
SomeClass.h). The major exception to this is functions that are just one
statement, such as getter and setter functions, which are usually small
enough to fit into the header file.
This makes for a modular program setup: main is in some file like
main.cpp, and SomeClass is defined in SomeClass.h and SomeClass.cpp.
You must then compile both main.cpp and SomeClass.cpp and link them
together to get the final executable.
Problem 6
Define an Array class that expands the functionality of C++ arrays. Show
how you would split your definition into an Array.h file and an Array.cpp
file.
The class should contain one data member that is a pointer to a dynamically
allocated array of integers, and an integer that stores the current size of the
array. The size integer should be const – it should not be changeable after
the class constructor is called.
The class should have 3 constructors: one that takes just a number of
elements to allocate (initializing them all to 0), another that takes a
number of elements and an array of initial elements, and a copy
constructor that allocates a new array of the same size as the Array that is
being copied, and then copies the elements one by one.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Also define a destructor to deallocate the memory that was allocated to the
internal array when the
Array object is destroyed.
You do not need to define a main function that actually uses this class, but it
will presumably be useful for testing purposes to try creating a few Array
objects and manipulating them.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Solution
Problem 1
(a)
#include
<ctime>
class Time
{ // or long, or
time_t t; int
public:
Time( long initialTime = time(0) )
{
t = initialTime;
}
long getTime()
{
return t;
};
void setTime(long newTime)
{
t = newTime;
}; // or long, or int
};
(b)
#include <ctime>
class Time
{
time_t t; public:
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Time(time_t initialTime = time(0) ) : t(initialTime) {}
int getTime()
{
return t;
}
}
};
Problem 2
#inclu
de
<iostre
am>
using
names
pace
std;
class
Compl
ex
{
double
real,
imagina
ry;
public:
Complex(double realPart=0, double imagPart=0)
{real=realPart; imaginary=imagPart;}; double
getReal()
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
{
return real;
};
double getImaginary()
{
return imaginary;
};
void set(double realPart, double imagPart)
{
real = realPart;
imaginary =
imagPart;
};
Complex
add(const
Complex &obj)
const
{
return
Complex(real
+ obj.real,
imaginary +
obj.imaginary
);
};
Complex
subtract(const
Complex &obj)
const
{
return
Complex(real
- obj.real,
imaginary -
obj.imaginary
For any Homework related queries, Call us at : - +1 678 648 4277
); You can mail us at : - [email protected] or
}; reach us at : - https://www.cpphomeworkhelp.com/
};
};
Problem 3 (UNGRADED)
Problem 4
1.A base class’s public members are accessible within that base class
and anywhere that the program has a handle to an object of that base
class or to an object of one of its derived classes.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
4.When deriving a class from a base class with public inheritance, public
members of the base class become public members of the derived class,
and protected members of the base
class become protected members of the derived class.
Problem 5
#include <iostream>
#include <cmath> using
namespace std;
double Account::Credit(double
amountToCredit)
{
balance +=
amountToCredit;
return balance;
}
bool
Account::Debit(dou
ble amountToDebit)
{
if(balance >=
amountToDebit)
{
balance -=
amountToDebit;
return true;
}
else
{
cerr << "Debit amount exceeded
account balance."<<endl; return false;
}
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Account::Account(double
initBalance):balance(initBalance)
{
if(balance<0.0)
{
SavingsAccount::SavingsAccount(double
interestRate, double initBalance)
: Account(initBalance)
{
interestRate=interestRate;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
CheckingAccount::CheckingAccount(double transactionFee, double
initBalance)
: Account(initBalance)
{
fee = transactionFee;
}
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Array(int[] initialValues, int
numElements); Array(const Array &);
int getLength() { return size; }
int &getElement(const int);
const int &getElement(const int)
const; void print(const string
&separator);
~Array();
};
Array.cpp
#include
<iostream>
#include
<string>
#include
"Array.h" using
namespace std;
Array::Array(int
numElements) :
size(numElemen
ts)
{
internalArray = new
int[size]; for(int i = 0; i
< size; i++)
internalArray[i] =
0;
}
Array::Array(int
initialValues[], int
numElements) :
size(numElements)
For any Homework related queries, Call us at : - +1 678 648 4277
{ You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/
Array(int[] initialValues, int
numElements); Array(const Array
&);
int getLength() { return
size; } int
&getElement(const int);
const int &getElement(const int)
const; void print(const string
&separator);
~Array();
};
{
return internalArray[n];
}
Array::~Array()
{
delete[]
internalArray;
};
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at : - [email protected] or
reach us at : - https://www.cpphomeworkhelp.com/