Constructor and Destructor: Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CONSTRUCTOR AND DESTRUCTOR

PROGRAM:

#include<iostream.h>
#include<conio.h>
class length
{
private:
int feet,inches;
public:
length()
{
feet =0;
inches =0;
cout<<"\n\t CONSTRUCTOR\n";
}
length(int f,int i)
{
feet =f;
inches =i;
cout<< "\n\t ARGUMENT CONSTRUCTOR\n";
}
void print()
{
cout<<"\n\t LENGTH IS\t ";
cout<< feet<< " " <<inches;
cout<<"\n";
}
void sum(length a,length b)
{
feet = a.feet + b.feet;
inches = a.inches + b.inches;
if(inches >= 12)
{
inches = inches-12;
feet++;
}
}
~length()
{
cout<<"\n\t THIS FUNCTION DESTROYED\n";
}
};

int main()
{
cout<<"\n\t\t\t CONSTRUCTOR AND DESTRUCTOR ";
cout<<"\n\t\t\t **************************\n ";
length l1(8,7),l2(5,9),l3;
l3.sum(l1,l2);
l1.print();
l2.print();
l3.print();
return(0);
}



































OUTPUT:





CONSTRUCTOR AND DESTRUCTOR
*********************************

ARGUMENT CONSTRUCTOR

ARGUMENT CONSTRUCTOR

CONSTRUCTOR

THIS FUNCTION DESTROYED

THIS FUNCTION DESTROYED

LENGTH IS 8 7

LENGTH IS 5 9

LENGTH IS 14 4

THIS FUNCTION DESTROYED

THIS FUNCTION DESTROYED

THIS FUNCTION DESTROYED

















STATIC MEMBER FUNCTION

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class tral
{
private:
static int able,tot;
public:
static void sumdata()
{
if(able!=0)
cout<<"\n THE AVAILABLE SEATS ARE : "<<able;
else
cout<<"\n THE SEATS ARE NOT AVAILABLE ";
}
static void sumdata1()
{
tot++;
able--;
if(tot<=60)
{
cout<<"\n YOUR SEAT IS RESERVED SUCCESSFULLY";
cout<<"\n THE SEAT NUMBER IS :"<<tot;
cout<<"\n REMAINING SEATS ARE : "<<able;
}
}
};
int tral::able = 60;
int tral::tot = 0;
void main()
{
int ch;
char cho;
clrscr();
cout<<"\n\t\t\t **********************";
cout<<"\n\t\t\t STATIC MEMBER FUNCTION ";
cout<<"\n\t\t\t **********************\n";
cout<<"\n\t\t\t TRAVEL AGENCY";
cout<<"\n\t\t\t ************* ";
cout<<"\n 1.ENQUIRY \n 2.RESERVATION \n 3.EXIT";
do
{
cout<<"\n ENTER YOUR CHOICE :";
cin>>ch;
switch(ch)
{
case 1:
tral::sumdata();
break;
case 2:
tral::sumdata1();
break;
case 3:
exit(0);
break;
}
cout<<"\n DO U WANT TO CONTINUE (Y/N)";
cin>>cho;
}while(cho=='y'|| cho=='Y');
}


























OUTPUT:



***************************
STATIC MEMBER FUNCTION
***************************

TRAVEL AGENCY
****************
1.ENQUIRY
2.RESERVATION
3.EXIT
ENTER YOUR CHOICE :1

THE AVAILABLE SEATS ARE : 60
DO U WANT TO CONTINUE (Y/N)y

ENTER YOUR CHOICE :2

YOUR SEAT IS RESERVED SUCCESSFULLY
THE SEAT NUMBER IS :1
REMAINING SEATS ARE : 59
DO U WANT TO CONTINUE (Y/N)Y

ENTER YOUR CHOICE :3





















#include <iostream>
using namespace std;

struct X {
private:
int i;
static int si;
public:
void set_i(int arg) { i = arg; }
static void set_si(int arg) { si = arg; }

void print_i() {
cout << "Value of i = " << i << endl;
cout << "Again, value of i = " << this->i << endl;
}

static void print_si() {
cout << "Value of si = " << si << endl;
// cout << "Again, value of si = " << this->si << endl;
}

};

int X::si = 77; // Initialize static data member

int main() {
X xobj;
xobj.set_i(11);
xobj.print_i();

// static data members and functions belong to the class and
// can be accessed without using an object of class X
X::print_si();
X::set_si(22);
X::print_si();
}
The following is the output of the above example:
Value of i = 11
Again, value of i = 11
Value of si = 77
Value of si = 22















#include <iostream>
using namespace std;

class X
{
public:
static int count;
};
int X::count = 10; // define static data member

int main ()
{
int X = 0; // hides class type X
cout << X::count << endl; // use static member of class X
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy