LectureN 3
LectureN 3
Computer
Programming
and
Applications
Classes
Constructors
and
Destructors
Lecture N+3
MA112 -
Computer
Programming
and
Applications
Classes
Constructors
and
Destructors 1 Classes
MA112 -
Computer
Programming
and #include <iostream>
Applications
class Test
{
Classes private:
Constructors int data1;
and
Destructors float data2;
public:
void insertIntegerData(int d)
{
data1 = d;
std::cout << "Number: " << data1;
}
example 3
MA112 -
Computer
Programming
and
Applications
Classes
float insertFloatData()
Constructors
and {
Destructors
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};
example 3
MA112 -
Computer
Programming
and
Applications
int main()
Classes {
Constructors Test o1, o2;
and
Destructors float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
MA112 -
Computer #include <iostream>
Programming
and
#include <string>
Applications class Stock // class declaration
{
Classes private:
Constructors std::string company;
and
Destructors
long shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
void acquire(const std::string & co, long n,
double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
}; // note semicolon at the end
example 4
MA112 -
Computer
Programming void Stock::acquire(const std::string & co, long n,
and
Applications double pr)
{
Classes company = co;
Constructors if (n < 0)
and {
Destructors
std::cout <<"Number of shares can’t be negative;"
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
example 4
MA112 -
Computer
Programming void Stock::buy(long num, double price)
and
Applications {
if (num < 0)
Classes {
Constructors std::cout <<"Number of shares purchased can’t be
and negative."
Destructors
<< "Transaction is aborted.\n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
example 4
MA112 -
Computer void Stock::sell(long num, double price)
Programming
and
{
Applications using std::cout;
if (num < 0)
Classes {
Constructors cout <<"Number of shares sold can’t be negative."
and
Destructors
<< "Transaction is aborted.\n";
}
else if (num > shares)
{
cout << "You can’t sell more than you have!"
<< "Transaction is aborted.\n";
}
else
{
shares -= num;
share_val = price;
set_tot(); } }
example 4
MA112 -
Computer
Programming
and
Applications void Stock::update(double price)
{
Classes share_val = price;
Constructors set_tot();
and
Destructors }
void Stock::show()
{
std::cout << "Company: " << company <<
" Shares: " << shares << std::endl<<
" Share Price: $" << share_val
<< " Total Worth: $" << total_val << std::endl;
}
example 4
MA112 -
Computer
Programming int main()
and
Applications {
Stock fluffy_the_cat;
Classes fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
Constructors fluffy_the_cat.show();
and fluffy_the_cat.buy(15, 18.125);
Destructors
fluffy_the_cat.show();
fluffy_the_cat.sell(400, 20.00);
fluffy_the_cat.show();
fluffy_the_cat.buy(300000,40.125);
fluffy_the_cat.show();
fluffy_the_cat.sell(300000,0.125);
fluffy_the_cat.show();
return 0;
}
512 Chapter 10 Objects and Classes
Constructors
and class Stock
Destructors {
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
void acquire(const char * co, int n, double pr);
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show();
};
MA112 -
Computer Stock kate("Woof, Inc.", 100, 63);
Programming Stock joe("Pryal Co.", 120, 30);
and
Applications
void Stock::show(void)
{
cout << "Company: " << company ...
...
}
kate.show(); joe.show();
MA112 -
Computer
Programming
and
Applications
Classes
Constructors 1 Classes
and
Destructors
You want
Default the function to return a new string, so its type is char*, or pointer-to-c
arguments
You want to leave the original string unaltered, so you use the const qualifier for the
argument.You want n to have a default value of 1, so you assign that value to n.A def
MA112 - argument value is an initialization value.Thus, the preceding prototype initializes n to
Computer value 1. If you leave n alone, it has the value 1, but if you pass an argument, the new v
Programming
and overwrites the 1.
Applications When you use a function with an argument list, you must add defaults from right
left.That is, you can’t provide a default value for a particular argument unless you also
Classes
vide defaults for all the arguments to its right:
Constructors
int harpo(int n, int m = 4, int j = 5); // VALID
and int chico(int n, int m = 6, int j); // INVALID
Destructors int groucho(int k = 1, int m = 2, int n = 3); // VALID
For example, the harpo() prototype permits calls with one, two, or three argumen
beeps = harpo(2); // same as harpo(2,4,5)
beeps = harpo(1,8); // same as harpo(1,8,5)
beeps = harpo (8,7,6); // no default arguments used
The actual arguments are assigned to the corresponding formal arguments from le
right; you can’t skip over arguments.Thus, the following isn’t allowed:
beeps = harpo(3, ,8); // invalid, doesn't set m to 4