0% found this document useful (0 votes)
18 views16 pages

LectureN 3

Uploaded by

yogapriya.kv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

LectureN 3

Uploaded by

yogapriya.kv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MA112 -

Computer
Programming
and
Applications

Classes MA112 - Computer Programming and


Constructors
and Applications
Destructors

Indian Institute of Space Science and Technology

November 02, 2023


MA112 -
Computer
Programming
and
Applications

Classes

Constructors
and
Destructors
Lecture N+3
MA112 -
Computer
Programming
and
Applications

Classes

Constructors
and
Destructors 1 Classes

2 Constructors and Destructors


example 3

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();

cout << "You entered " << secondDataOfObject2;


return 0;
}
example 4

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

MA112 - keyword private identifies class members


Computer that can be accessed only through the public
Programming member functions (data hiding)
and
keyword class the class name becomes the
Applications

identifies name of this user-defined type class members can be


class definition data types or functions
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();
};

keyword public identifies class members


that constitute the public interface for
the class (abstraction)
Abstraction and Classes 51

MA112 -
Computer Stock kate("Woof, Inc.", 100, 63);
Programming Stock joe("Pryal Co.", 120, 30);
and
Applications

Woof, Inc. creates two objects, Pryal Co.


Classes
100 each with its own 120
Constructors 63 data, but uses just 30
and 6300 one set of member 3600
Destructors functions
kate joe

void Stock::show(void)
{
cout << "Company: " << company ...
...
}

show() member function

kate.show(); joe.show();

uses show() member uses show() member


function with kate data function with joe data

Figure 10.2 Objects, data, and member functions.


Lecture N+3

MA112 -
Computer
Programming
and
Applications

Classes

Constructors 1 Classes
and
Destructors

2 Constructors and Destructors


char * left(const char * str, int n = 1);

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

Default arguments aren’t a major programming breakthrough; rather, they are a co


venience.When you begin working with class design, you’ll find that they can reduce
number of constructors, methods, and method overloads you have to define.

You might also like