Source Code:
#include<iostream>
using namespace std;
class tollBooth{
private:
unsigned int cars;
float amount;
public:
tollBooth():cars(0),amount(0)
{ }
void payingCar(){
cars++;
amount=amount+0.50;
}
void nopayingCar(){
cars++;
amount=amount;
}
void display(){
cout<<"Number of cars passed: "<<cars<<endl;
cout<<"Total Amount: "<<amount<<endl;
}
};
int main (){
tollBooth c1;
int selection;
cout<<"Press 1 for Paying Car Counter\n";
cout<<"Press 2 for NON-Paying Car Counter\n";
cout<<"Press 3 for Display\n";
cout<<"Press 4 for Exit\n\n";
do{
cout<<"Input: ";
cin>>selection;
switch(selection)
{
case 1:
c1.payingCar();
c1.display();
break;
case 2:
c1.nopayingCar();
c1.display();
case 3:
c1.display();
}
cout<<"\n";
} while(selection!=4);
}
Wednesday, 22 March 2017
Example of Default Constructor (Book Management Program)
Source Code:
#include<iostream>
using namespace std;
class book{
private:
string name;
int id;
int pages;
int price;
public:
book(): name("ICT"), id(101), pages(200), price(500){}
void dis()
{
cout<<"Book Details: \n";
cout<<"ID"<<"\t\t"<<"Name"<<"\t\t"<<"Pages"<<"\t\t"<<"Price\n";
cout<<id<<"\t\t"<<name<<"\t\t"<<pages<<"\t\t"<<price;
}
};
int main ()
{
book b1;
b1.dis();
}
#include<iostream>
using namespace std;
class book{
private:
string name;
int id;
int pages;
int price;
public:
book(): name("ICT"), id(101), pages(200), price(500){}
void dis()
{
cout<<"Book Details: \n";
cout<<"ID"<<"\t\t"<<"Name"<<"\t\t"<<"Pages"<<"\t\t"<<"Price\n";
cout<<id<<"\t\t"<<name<<"\t\t"<<pages<<"\t\t"<<price;
}
};
int main ()
{
book b1;
b1.dis();
}
What is constructor in C++?
Constructor:
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
Tuesday, 21 March 2017
Simple program created using Class (Object Oriented Programming)
This program will make us understand, how to use class and what is Object Oriented Programming. This program will take 6 strings Input and will display it on the screen as Output.
Source Code:
#include<iostream>
using namespace std;
class CourseNames
{
private:
string Names[6];
public:
void GetData()
{
cout<<"Enter Names of your Courses: "<<endl<<endl;
for(int i=0; i<6; i++)
{
getline(cin, Names[i]);
}
cout<<endl;
}
void DisplayData()
{
cout<<"The course you Entered: "<<endl;
for(int j=0; j<6; j++)
{
cout<<"\n*"<<Names[j]<<endl;
}
}
};
int main ()
{
CourseNames S1;
S1.GetData();
S1.DisplayData();
}
Subscribe to:
Posts (Atom)