Classes, Objects, Friend Function, Constructors & Destructors
Classes, Objects, Friend Function, Constructors & Destructors
1
Is there any need for “Classes” ??
2
The concept of a Class
int struct item class item
{ {
int code; int code;
float price; float price;
}; public:
void getdata();
void display();
};
void item :: getdata()
int x; struct item x; {
………..
x=5; x.code = 234; }
cout << x; x.price = 45.50; void item :: display()
{
………..
}
item x;
x.code;
x.putdata();
x.display(); 3
Programming with Classes
Define a Class
Create objects
4
1. Defining a Class
5
2. Defining Member functions
class class_name class item
{ {
private: private:
int code;
variable declaration;
float price;
function declaration; public:
public: void getdata();
variable declaration; void display();
function declaration; };
};
void item :: getdata(void)
{
ret-type class-name :: fn-name (arguments)
cout << “Enter code & price”;
{ cin << code << price;
function body; }
}
void item :: display(void)
{
cout << “code:” << code;
cout << “price:” << price;
}
6
2. Defining Member functions inside the Class
class item
{
private:
int code;
float price;
public:
void getdata();
{
cout << “Enter code & price”;
cin << code << price;
}
void display();
};
/*==============================*/
OUTPUT
void item :: getdata(void){
Enter code: 324
cout << “\n\n Enter code: “;
Enter price: 34.50
cin >> code;
cout <<“\n\t Enter price: “; Code = 324
cin >> price; Price = 34.50 9
}
Another Example with Class
// Program: ex2.cpp
int main()
# include<iostream.h>
{
using namespace std;
person p;
claass person { p.getdata();
p.display();
char name[20]; return (0);
int age;
public:
void getdata(void) //function defination
{
cout << “\n\n Enter name: “;
cin >> name;
cout <<“\n\t Enter age: “;
cin >> age;
}
void display(void);
} OUTPUT
/*=============================================*/
void person :: display(void) Enter name: XYZ
{ Enter age: 26
cout << “\n\t Name = “ << name;
Name = XYZ
cout << “\n\t Age = “ << age;
} Age = 26 10
Nesting Member functions
// Program: ex3.cpp int item :: largest(void)
{
# include<iostream.h> (m>n) ? Return(m) : return(n);
using namespace std; }
18
Constructors…Is there any need ??
// Program: ex1.cpp void item :: volume(void)
/* Demonstrates the need of {
constructors */ cout << “ The volume of Box = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;
/*================================*/
class Box {
int main()
double width; {
double height, depth; Box b;
public: b.set_dim();
void set_dim(void); b.volume();
void volume(void); return (0);
};
/*==============================*/
cout << “Enter width, height Enter width, height and depth of the Box
and depth of the 2.3 4.1 2.0
Box“;
cin >> width >> height; The volume of Box = 18.86
cin >> depth; 19
}
Constructors…An Example
// Program: ex2.cpp void item :: volume(void)
/* Demonstrates the use of {
constructors */ cout << “ The volume of Box = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;
/*================================*/
class Box {
int main()
double width; {
double height, depth; Box b;
public: b.volume();
void volume(void); return (0);
};
/*==============================*/
Box :: Box(void)
{ OUTPUT
int main()
double width;
{
double height, depth;
Box B1(2.3, 4.1, 2.0);
Box B2(1.0, 1.0, 1.0);
public: B1.volume();
Box :: Box(double w, double h, double d) B2.volume();
{ return (0);
width = w;
height = h;
depth = d;
OUTPUT
}
/*====================================*/
Box :: Box(double w, double h, double d)
{
1. Create a class to find the sum of two numbers. This includes two data
members, three member functions to input, output the data members
and to find the sum of two data members.
2. Create a class to find the multiplication of two numbers. This include
three data member of type arrays, three member functions, one is to
accept two matrices, other to display two matrices and 3rd one is to find
the multiplication of two matrices and display the result.
3. Create a struct declaration with a single member function; then create a
definition for that member function. Create an object of your new data
type, and call the member function.
4. Write a class to represent a vector (a series of float values). Include
member functions to perform the following tasks –
• To create the vector
• To modify the value of the given element
• To multiply by a scalar vector
• To display the vector in the form (10,20,30,...).
5. Modify the class and program of previous question such that the
program would be able to add two vector and display the resultant
vector.
6. Exercise 5.1- 5.5 and 6.1 - 6.5 from Balagurusamy
27