Project on Ticket Booking System Using C++

33
Presented by : Md. Asad Hossain Roll no. : 0903087 Sec. B , Group : B1 Dept. of EEE, KUET

description

goes

Transcript of Project on Ticket Booking System Using C++

Page 1: Project on Ticket Booking System Using C++

Presented by :Md. Asad HossainRoll no. : 0903087Sec. B , Group : B1Dept. of EEE, KUET

Page 2: Project on Ticket Booking System Using C++

To study on Object-Oriented Programming To study on Basic concepts of Object-Oriented Programming To study on Member function To study on Inline functions To study on Friendly Functions To study on CONSTRUCTORS AND DESTRUCTORS To study on OPERATOR OVERLOADING To study on INHERITANCE To study on POINTERS To study on Working with files

Page 3: Project on Ticket Booking System Using C++

Object oriented programming is an approach to program organization and development that attempts to eliminate some of the pitfalls of conventional programming methods.

C++ : C++ is an object oriented programming language. It was developed at AT&T Bell

Laboratories in Murray Hill,New Jersey, USA,in the early 1980’s. In November 1997, the ANSI standards committee standardised these changes and added new features to the language specifications. C++ is a superset of C. Most of what we already know about C applies to C++ also. The most important facilities that C++ adds on to classes, inheritance, function overloading and operator overloading.The object oriented features in c++ allow programmers to build large programs with clarity , extensibility and eace of maintenance incorporating spirit and efficiency of C.

Page 4: Project on Ticket Booking System Using C++

# Objects# Classes# Data abstraction and encapsulation# Inheritance# Polymorphism

Page 5: Project on Ticket Booking System Using C++

Objects: An object represents an individual, identifiable item, unit,

or entity, either real or abstract, with a well-defined role in the problem domain.

Objects are the basic run time entities in an object-oriented system.They may represent a person,a place,a bank account,a table of data or any item that the program has to handle.They may also represent user-defined data such as vectors,time and lists.

In C++, the class variables are called objects. With objects we can access the public members of a class using a dot operator.

class item { ............ ............. ............. } x,y,z;Here item is an object

Page 6: Project on Ticket Booking System Using C++

Classes: A class is an extension of the idea of structure used in C. A class can have

both variables and functions as members. The only difference between a structure & a class in c++ is that , by default the members of a class are private by default , the members of a structure are

public.Generally a class specification has two parts :

• Class declaration• Class function definitions

A simple class example:

class item{int number;float cost;public :void getdata(inta ,float b);void put data(void) };

Page 7: Project on Ticket Booking System Using C++

Data Abstraction and Encapsulation:

The wrapping up of data and functions into a single unit is known as encapsulation.

Abstraction refers to the act of representing essential features without including the background details or explanations.

Page 8: Project on Ticket Booking System Using C++

Polymorphism: Polymorphism is the ability to take more

than one form. The process of making an operator to exhibit

different behaviors in different instances is known as operator overloading.

Inheritance: Inheritance is the process by which objects of one

class acquire the properties of objects of another class

Page 9: Project on Ticket Booking System Using C++

Since C++ allows us to create hierarchy related objects, we can build special object oriented libraries which can be used later by many programmers.

While C++ is able to map the real-world problem property , the C part of C++ gives the language the ability to get close to the machine level details.

C++ programs are easily maintainable and expandable. It is expected that C++ will replace C as a general purpose

language in the near future.

Page 10: Project on Ticket Booking System Using C++

# include <iostream># include <conio.h>void main(){cout<<”C++ Programming.”;getch();}

Page 11: Project on Ticket Booking System Using C++

The functions declared inside the class is known as member function.

Defining member functions :The general form of member function definition is:

return type class-name :: function-name (argument declaration){function body} Characteristics of member functions:# Several different classes can use the same function name . The “member label” will resolve their scope.# Member functions can access the private data of the class. A non-member function can’t do so.# A member function can call another member function directly, without using the dot operator.

Page 12: Project on Ticket Booking System Using C++

Functions in C++Like C in C++ there is a ‘main’ function in each program.Function prototyping is one of the major improvements added to C++ functions.Every function can be overloaded.Small functions can be made inline to save time and memory space.

Function prototypingFunction prototyping is a declaration statement in the calling program and is of the following form:type function-name(argument-list);Example : float area(float x,float y);

Page 13: Project on Ticket Booking System Using C++

Inline functionsAn inline function is a function that is expanded in line when it is invoked. The

general format of the inline functions is

inline return_type function_name(argument)

{

//Function body

}

It’s purpose is to eliminate the cost of time of calls to small functions.

Page 14: Project on Ticket Booking System Using C++

INLINE FUNCTION

#include<iostream.h>#include<conio.h>inline float mul(float x, float y){return(x*y);}inline double div(double p, double q){return(x/y);}int main(){float a=12.345;cloat b=9.82;cout<<mul(a,b)<<“\n”;cout<<div(a,b)<<“\n”;getch();}

121.2281.25713

Overhead of a function and return is eliminated here

Page 15: Project on Ticket Booking System Using C++

As we know that the private members can’t be accessed from outside the class.That is a non-member function can’t have an access to the private data of a class.But C++ allows using “friend function” to have access to the private data of both classes.

The function that are declared with the keyword ‘friend’ are known as friend function.

To make an outside function “friendly” to a class,we have to simply declare this

function as a friend of the class as shown below:

Class Item{……..……..Public:……..friend void ABC(void); //declaration};

Page 16: Project on Ticket Booking System Using C++

Friend Function

#include<iostream.h>#include<conio.h>class sample{int a;int b;public:void setvalue() {a=25; b=40;}friend float mean(sample s);};float mean(sample s){return float(s.a+s.b)/2.0;}int main(){sample X; //object XX.setvalue();cout<< "Mean value="<<mean(X);getch();}

The friend function access the class variables a and b by using the dot operator.

Page 17: Project on Ticket Booking System Using C++

Constructors: A constructor is a special member function whose task is to initialize the objects of its class. It constructs the values of data members of the class.

The general format of defining & declaring constructor:

class integer {Int m,n;public:integer(void); //constructor declared.............................};integer :: integer (void) // constructor defined{m=0;n=0;}

An object created by the class will be initialized automatically.

integer int1; Not only creates the object int1 of type integer but also initializes its data members m and n to zero.

Page 18: Project on Ticket Booking System Using C++

Copy constructor :A copy constructor is used to declare and initialize an object from another object.Integer I2(I1);

Destructors:

A destructor, as the name implies, is used to destroy the objects that have been created by constructor. The general format of destructor is given bellow:

~integer(){}

For example, the destructor for the matrix class may be defined as follows:

matrix :: ~matrix(){for (int i=0;i<d1;i++)delete p[i];delete p;}

Page 19: Project on Ticket Booking System Using C++

Operator overloading:

Operator overloading is one of the important features of C++ language. It is called compile time polymorphism. Operator overloading is done with a special function, called operator function.

The general format of operator function is:

return type class-name :: operator op(arg-list){function body //task defined}

Operator being overloaded

Page 20: Project on Ticket Booking System Using C++

Requirements:# Creating a class that defines the data type that is to be

used in the overloading operation.# Declearing the operator function operator op() in the public

part of the class.It may be either a member function or a friend function.

# Define the operator function to implement the required operators.

We cann’t overload :

*class member access operator (.,.*)*scope resulation operator (::)*size operator (sizeof)*conditional operator(?:)

Page 21: Project on Ticket Booking System Using C++

#include<iostream.h>#include<conio.h>class space{int x;int y;int z;public:Void getdata(inta,intb,int c);void display();void operator-(); //overload unary minus.(declaration)}void space :: getdata(inta,intb,int c){x=a;y=b;z=c;}void space :: display(){cout<<"x="<<x<<"\n";cout<<"y="<<y<<"\n";cout<<"z="<<z<<"\n\n";}void space :: operator-(){x=-x;y=-y;z=-z;}int main(){space s;s.getdata(10,-20,30);cout<<"S:";s.display();-s; activates operator-() functioncout<<"-S";s.display();getch();return 0;}

Definitions of operator- function

Page 22: Project on Ticket Booking System Using C++

The mechanism of deriving a new class from an old class is called inheritance. Inheritance provides the concept of reusability. The different types of inheritances are given below: #Single InheritanceA derived class with only one base class is called single inheritance. A→B

#Multiple InheritanceA class can inherit properties from more than one class which is known as multiple inheritance. A→C←B

#Multilevel InheritanceA class can be derived from another derived class which is known as multilevel Inheritance. A→B→C

#Hierarhcical inheritance: One class may be inherited by more than one class.

Page 23: Project on Ticket Booking System Using C++

The general format of defining derived classes

class derived class-name : (visibility mode) base class-name {…………………// members of derived class…………};

Page 24: Project on Ticket Booking System Using C++

#include<iostream.h>#include<conio.h>class B{int a;public:int b;void get_ab();int get_a(void);void show_a(void);

};class D:private B{int c;public:void mul(void);void display (void);};void B::get_ab(void){cout<<"enter values for a and b" ;cin>>a>>b;}

Base class

Inheritance declared

Page 25: Project on Ticket Booking System Using C++

int B::get_a(){return a;}void B::show_a(){cout<<"a="<<a<<"\n";}

void D::mul(){get_ab();c=b*get_a();

}

void D::display()

{show_a();cout<<"b="<<b<<"\n";cout<<"c="<<c<<"\n";}int main(){D d;d.mul();d.display();d.mul();d.display();getch();return 0;}

Page 26: Project on Ticket Booking System Using C++

A pointer is a variable that contains an address which is location of another variable in the memory.It is a derived data type that refers to another data variable by storing the variable’s memory address rather than data. A pointer variable defines where to get the value of a specific data variable instead of defining actual data.

The general format of pointer variable

data-type *pointer-variable;

Page 27: Project on Ticket Booking System Using C++

C++ uses a unique keyword called this to represent an object that invokes a member function. this is a pointer that points to the object for which this function was called . This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions.

Consider the following simple example

class ABC{int a;............};

The private variable “a” can be used directly inside a member function, likea=123;

Page 28: Project on Ticket Booking System Using C++

A simple program using this pointer#include<iostram.h>#include<cstring.h>#include<conio.h>class person{char name[20];float age;public:person(char *s,float a){strcpy (name,s);age=a;}person& person :: greater(person & x){If(x.age>=age)return x;elsereturn *this;}void display(){cout<<”name:”<<name<<”\n”;cout<<”age:”<<age<<”\n”;}};void main(){person p1(“Jhon”,37.50),p2(“ahmed”,29.0),p3(“hebber”,40.25);person p=p1.greater(p3);cout<<”Elder person is:”<<”\n”;p.display();p=p1.greater(p2);cout<<”Elder person is:”<<”\n”;p.display();getch();}

Page 29: Project on Ticket Booking System Using C++

C++ streamsA stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent.C++ stream classes The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disc files. This classes are called C++ streams classes

Page 30: Project on Ticket Booking System Using C++

A program typically involves either or both of the following kinds of data communication:# Data transfer between the console unit & the program.# Data transfer between the program & a disk file.

opening a file :

1.Using the constructor function2.Using the member function open() of the class

Opening file using constructor :

# Create a file stream object to manage the stream using the class ofstream to create output stream & the class ifstream to create the input stream.# Initialize the file object with the desired filename.

For example the following statement opens a file named “reasults” for output Ofstream outfile(“results”); // output only

Similarly for input:

Ifstream infile(“data”); // input only

Page 31: Project on Ticket Booking System Using C++

The file stream classes support the following functions to manage :•seekg() Moves get pointer to a specified location.•seekp() Moves put pointer to a specified location.•tellg() Gives the current position of the get pointer.•tellp() Gives the current position of the PUT pointer.

Page 32: Project on Ticket Booking System Using C++

#include<iostream.h>#include<fstream.h>#include<conio.h>void main(){ofstream outf(“ITEM”);cout<<”Enter item name:”;char name[30];cin>>name;outf<<name<<”\n”;cout<<”Enter item cost:”;float cost;cin>>cost;outf<<cost<<”\n”;outf.close();Ifstream inf(“ITEM”);inf>>name;inf>>cost;cout<<”\n”;cout<<”Item name:”<<name<<”\n”;cout<<”Item cost:”<<cost<<”\n”;inf.close();getch();}

Write data on file

read data on file

Provides support for simultaneous i/o

Enter item name : CD-REnter item cost : 120item name : CD-Ritem cost : 120

Page 33: Project on Ticket Booking System Using C++

Thanks to all