Classes and Objects
Simple C++ Program using Classes and Objects- INTRO
class HelloWorld
{
private: int a; char b; Data Members
public:
void getInput()
Member Functions
{
cin>>a>>b;
cout<<a<<b;
}
void displayMessage()
{ Access specifiers:
cout << “Hello World!" << endl; Public
} Private
}; Protected
int main()
{ Member functions:
HelloWorld object1; Normal functions
object1.displayMessage(); Constructors
object1.getInput(); Destructors
return 0;
}
What is a Class?
• Class: The building block of C++ that leads to Object Oriented
programming
• It is a user defined data type, which holds its own data members
and member functions, which can be accessed and used by
creating an instance (object) of that class.
Data members are the data variables (like int a; char b; etc)
Member functions are the functions used to manipulate
these variables (like normal functions that we create)
Object?
An Object is an instance of a Class.
When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created)
memory is allocated.
Once a class is defined, it can be used to create
variables of its type known as objects. The
relation between an object and a class is the
same as that of a variable and its data type.
Example : Declaring objects of a class
class book
{
II body of the class
};
int main ()
{
book book1, book2, book3; //objects of class book
return 0;
}
Another Way
class book
{
// body of class
}bookl,book2,book3; // declaration of objects
Memory Allocation
Lets consider the Scenario of creating a PECStudent
Class
• class PECStudent
• Objects (instances of the class): Student1,
Student2, Student3 etc
• Data members of this class can be Name, Roll
Number, Marks etc.
• Member Functions: Average Marks, Highest
Marks etc
#include <iostream>
#include<cstring> How to access the data members of
using namespace std;
a class inside main function ??
class PECStudent
{
public:
string name;
int studentID;
};
int main() Data Members
{
PECStudent student1; //first object
PECStudent student2; //second object
student1.name=“Raju";
student1.studentID = 123456789;
student2.name="XYZ";
student2.studentID = 987654321;
cout<<"Student1 name is "<<student1.name<<endl;
cout << "student1 id is" << student1.studentID << endl;
cout<<"Student2 name is "<<student2.name<<endl;
cout << "student2 id is" << student2.studentID << endl;
return 0; }
SYNTAX OF A CLASS
class classname
{
Access specifier: //can be private, public, protected
Data members; //variables to be used
Member functions() { } //to access and operate on data members
}; //classname ends with semicolon
#include<iostream>
#include<cstring> Defining the function outside of class
using namespace std;
class HelloWorld
{
public:
void displayMessage(string displayMsg);
};
void HelloWorld:: displayMessage(string displayMsg)
{
cout <<displayMsg << endl;
}
int main()
{
string displayMsg;
cin>>displayMsg;
HelloWorld object1;
object1.displayMessage(displayMsg);
return 0;
}
class Person
{ Create a Person class to get
char name[30]; display name and Age
int age;
public:
void getdata();
void display();
}; int main()
void Person:: getdata() {
{ cout <<"Enter Name: "; Person p;
cin>>name; p.getdata();
cout<<"Enter Age: "; p.display();
cin>>age; } return 0;
void Person:: display() }
{ cout <<name<<endl;
cout <<age; }
Constructor and Destructor
What is a Constructor
• It is a member function which initializes a
class.
• A constructor has:
(i) the same name as the class itself
(ii) no return type
Creating a member function in class
Create a class to display complex numbers
(a+bi)
3+4i
3-4i = 3+ (-4i)
1+2i
1-2i = 1+(-2i)
etc
#include <iostream>
int main()
using namespace std;
{
class Ncomplex
Ncomplex c1;
{
c1.display();
private:
return 0;
int nReal;
int nImg; }
char c='i', d='+';
public:
When you run this
void display()
program,
{
Some garbage values will
cout<<nReal<<d<<nImg<<c;
be assigned by default
}
class constructor to
};
nReal and nImg,
These values will be
printed
#include <iostream>
using namespace std; Passing values to the function
class Ncomplex
{
10+20i
public:
int nReal;
int nImg; int main()
char c='i', d='+'; {
void display(int x, int y) Ncomplex c1;
{ c1.display(10,20);
nReal=x; return 0;
nImg=y; }
cout<<nReal<<d<<nImg<<c;
}
};
class Ncomplex
Initializing the values of nReal and nImg
{ Using constructor
private:
int nReal;
int nImg;
char a=‘+’,b=‘i’; int main()
public: {
Ncomplex() Ncomplex c1;
{
c1.display();
nReal=0;
nImg=0; return 0;
} }
void display()
{
cout<<nReal<<a<<nImg<<b;
}
};
class Ncomplex
{ Parameterized Constructor
private:
int nReal;
int main()
int nImg;
{
char c='i', d='+';
public: Ncomplex c1(3,4);
Ncomplex(int x, int y) c1.display();
{ return 0;
nReal=x; }
nImg=y;
}
void display()
{
cout<<nReal<<c<<nImg<<d;
}
};
Overloading Constructors
• You can have more than one constructor in a class, as
long as each has a different list of arguments.
class rectangle { int main()
private: {
rectangle rc1(3.0, 2.0);
float height; rectangle rc2();
float width;
int xpos; rc1.draw();
int ypos; rc2.draw();
return 0;
}
public:
rectangle(float, float); // constructor
rectangle(); // another constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
};
TRY THIS PROGRAM
Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50
cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth
keeps track of the number of cars that have gone by, and of the total amount of
money collected.
Model this tollbooth with a class called tollBooth. The two data items are a type
unsigned int to hold the total number of cars, and a type double to hold the total
amount of money collected. A constructor initializes both of these to 0. A member
function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(), increments the car total but adds nothing to the
cash total. Finally, a member function called display() displays the two totals.
Include a program to test this class. This program should allow the user to push one
key to count a paying car, and another to count a non-paying car. Pushing the Esc key
should cause the program to print out the total cars and total cash and then exit.