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);
}
C++ Programming
Programs Created by: Umair Mushtaq
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();
}
Thursday, 16 February 2017
Structure (DataBase) with File Handling
#include<iostream>
#include<fstream>
using namespace std;
struct database{
char name[10];
char phone[12];
char email[30];
float height;
int salary;
}p[3];
int main(){
for (int n=1;n<=3;n++)
{
cout<<"Enter Record of Person: "<<n<<endl;
cout<<"Name: ";
cin>>p[n].name;
cout<<"Phone: ";
cin>>p[n].phone;
cout<<"Email: ";
cin>>p[n].email;
cout<<"Height: ";
cin>>p[n].height;
cout<<"Salary: ";
cin>>p[n].salary;
cout<<endl;
}
fstream pdata;
pdata.open("DataBase.txt",ios::out);
if(!pdata)
{
cout<<"ERROR";
}
pdata<<"Name\t\t\tPhone\t\t\t\tEmail\t\t\t\tHeight\t\t\tSalary"<<endl;
for (int i=1;i<=3;i++)
{
pdata<<p[i].name<<"\t\t\t"<<p[i].phone<<"\t\t\t\t"<<p[i].email<<"\t\t\t\t"<<p[i].height<<"\t\t\t"<<p[i].salary<<"\t\t\t"<<endl;
}
}
#include<fstream>
using namespace std;
struct database{
char name[10];
char phone[12];
char email[30];
float height;
int salary;
}p[3];
int main(){
for (int n=1;n<=3;n++)
{
cout<<"Enter Record of Person: "<<n<<endl;
cout<<"Name: ";
cin>>p[n].name;
cout<<"Phone: ";
cin>>p[n].phone;
cout<<"Email: ";
cin>>p[n].email;
cout<<"Height: ";
cin>>p[n].height;
cout<<"Salary: ";
cin>>p[n].salary;
cout<<endl;
}
fstream pdata;
pdata.open("DataBase.txt",ios::out);
if(!pdata)
{
cout<<"ERROR";
}
pdata<<"Name\t\t\tPhone\t\t\t\tEmail\t\t\t\tHeight\t\t\tSalary"<<endl;
for (int i=1;i<=3;i++)
{
pdata<<p[i].name<<"\t\t\t"<<p[i].phone<<"\t\t\t\t"<<p[i].email<<"\t\t\t\t"<<p[i].height<<"\t\t\t"<<p[i].salary<<"\t\t\t"<<endl;
}
}
Saturday, 14 January 2017
How to find a number in an Array?
#include<iostream>
using namespace std;
int main ()
{
int arr[5]={55,66,77,98,12};
int n,location=-1;
cout<<"Enter a number to check whether it is in the array or not: ";
cin>>n;
for(int i=0;i<5;i++)
{
if(arr[i]==n)
{
location=i;
cout<<"The number you entered is in the Array...!!!"<<endl<<arr[location];
break;
}
}
if(location==-1)
{
cout<<"NOT FOUND";
}
}
using namespace std;
int main ()
{
int arr[5]={55,66,77,98,12};
int n,location=-1;
cout<<"Enter a number to check whether it is in the array or not: ";
cin>>n;
for(int i=0;i<5;i++)
{
if(arr[i]==n)
{
location=i;
cout<<"The number you entered is in the Array...!!!"<<endl<<arr[location];
break;
}
}
if(location==-1)
{
cout<<"NOT FOUND";
}
}
Saturday, 10 December 2016
Write a program that inputs a number from a user and displays its factorial. It asks the user whether he wants to calculate another factorial of not. It the user inputs 1, it again inputs number and calculates factorial. If User inputs 0, program terminates.
Source Code:
#include<iostream>
using namespace std;
int main ()
{
int n,x,fact=1;
do{
cout<<"Enter number to find Factorial: "<<endl;
cin>>n;
for (int i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial of your numbers: "<<fact<<endl;
cout<<"Do you want to continue? \nIf 'Yes' then press 1 or If 'No' then press 0..."<<endl;
cin>>x;
fact=1;
}
while(x==1);
}
Subscribe to:
Posts (Atom)