Вы находитесь на странице: 1из 3

CS101 Lab 7: Classes

What to do?
1. Read Sections 2 and 3. Refer to lecture slides/programs.
2. Solve problems in Section 4.

Classes
1. A Class defines a module in which you can pack some functions that
share variables.
2. You can create many instances from a class just as you can create
many values x,y,z,.. from types such as int, float, char...
3. A class conceals the state from external accesses. Only the member
functions which are members of the class can access these variables.
So we dont have to use global variables for sharing.
4. More, we can create many instances of a class. So at a time, there can
be many instances of the same class around, and each one keeps its
own copy of the state variables.
5. A class has many components. Today we shall deal with the following
major components of classes.
(a) name of the class
(b) private variables (mainly variables that define the state)
(c) public functions (mainly member functions which can be invoked
from outside the class)
(d) Constructor (initialization code. Each time an instance is
created, constructor is called automatically)
6. Member functions are like functions that we have worked with. They
take parameters and they return results. In addition to the
conventional input parameters and output results, they can also access
and modify variables private inside the class to which they belong.

Sample Class

//Class, and Creation and Use of its Instances:


#include<iostream>
using namespace std;
class Book { // name of the class
private: // all variables private
int bookID;
bool isIssued;
int issuedTo;
public: //public member functions
Book(int id); // constructor
bool issueBook(int memberID); // functions takes a param and returns a bool
void returnBook();
};
Book :: Book(int id){
bookID = id;
isIssued = false;
issuedTo=0;
}
bool Book :: issueBook(int memberID) {
if(isIssued){
cout<<"This book is already issued to Member no "<<issuedTo<<endl;
return false;
}
else {
isIssued=true;
issuedTo=memberID;
cout<<"Book is succesfully issued to Member no "<<issuedTo<<endl;
}
}
void Book :: returnBook() {
if(isIssued) {
isIssued = false;
issuedTo=0;
cout<<"Book is returned successfully"<<endl;
}
}
2

int main() {
bool result;
Book b1(2341);
// created one book object, 2341 is passed in as bookid
result = b1.issueBook(1001); // dot operator: member function invocation
result = b1.issueBook(1002); // 1002 is passed in as user id
result = b1.returnBook(); // this produces an error! fix it.
result = b1.issueBook(1002);
// we did not use result here, but you will need to use it to solve
// the problem given below.
}

Problems:
1. In the sample class discussed above, add a feature to mark books for
movement into reference section and back to issuing. Also remove
all cout statements from within the class into main. To do
this, make necessary changes to the program. All variables must
be private!
2. A Pop Memory object is designed to hold one number at most. The
Pop Memory is empty initially. You can insert one number in in when
the memory is empty. You can pop the number out when there is
already a number inserted inside. Develop a class that implements
this description of Pop Memory. Test your Pop Memory by creating
two or three instances of it in main() and by invoking member
functions on them.
Again no cins and couts inside the class definition. All
variables must be private!
3. Modify the vending machine class discussed in the lecture to
incorporate two more features of your choice. Test the additional
features.
No cins and couts inside the class. All variables private.

Вам также может понравиться