Inheritence

#include<iostream.h>
#include<string.h>

class Vehicle{
	double weight;
	int speed;


public:
	Vehicle(double w, int s):weight(w),speed(s){}

	void accelerate(){
		setSpeed( getSpeed() + 10);
	}
	void setSpeed(int s){
		speed += s;
	}
	int getSpeed(){
		return speed;
	}

};

class Car: public Vehicle{
	char model[10];

public:

	Car(double w, int s, char m[10]):Vehicle(w,s){
		strcpy(model,m);
	}

	void accelerate(){
		setSpeed(getSpeed() + 50);
	}
};

void main(){
	Vehicle generic(1000,100);
	Car mycar(800, 400, "McLaren");

	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

   	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;

   	generic.accelerate();
	mycar.accelerate();

   	cout<<"Generic: "<<generic.getSpeed()<<endl;
	cout<<"Car: " << mycar.getSpeed()<<endl;
}
Advertisements
Posted in Uncategorized | Leave a comment

Classes–A Deeper look


/*********************/
/*  Filename: Student.h    */
/*********************/


/*-------prototypes-------*/

class Student{

	//these are private by default
	int cbNo;
	char name[10];
	int total;
	char status;

public:
	char getGrade(); //get grade
	void getInfo(); //display student info
	char getStatus(); //return P or F
	void setTotal(int);

	Student();
	Student(int, char []); //parameterised constructor

};

/*********************/
/*  Filename: Student.cpp  */
/*********************/

#include<iostream>
#include<string>
#include "Student.h"

using namespace std;

/*-------implementations--------*/

Student::Student(){ //<which class>:: <which function>
	cbNo=0;
	strcpy_s(name, ""); //cant copy a string to another;

}

Student::Student(int cb, char n[]){
	cbNo = cb;
	strcpy_s(name, n); 
}

char Student::getGrade(){
	
	char grade;

	if(total >80){
		grade = 'A';
		status = 'P';
	} else if(total >70){
		grade = 'B';
		status = 'P';
	} else {
		grade = 'C';
		status = 'F';
	}
	return grade;
}

void Student::getInfo(){
	cout<<"CB:"<< cbNo<<endl;
	cout<<"Name: "<<name<<endl;
	cout<<"Grade: " << getGrade()<<endl;
	cout<<"Status: " <<getStatus()<<endl;
}
char Student::getStatus(){
	return status;
}

void Student::setTotal(int t){
	total = t;
}


int main(){

	Student std(3403, "Praveen");
	std.setTotal(90);
	std.getInfo();

	Student s;
	s.getInfo();

	system("pause");
	return 0;

}
Posted in Uncategorized | Tagged , | Leave a comment

Binary Search implementation in C++



#include<iostream>


using namespace std;

int binarySearch(int [], int find, int left, int right);

main()
{
      int arr[] = {-5, -2, 1, 3, 4, 6, 8, 9, 11};
      int search;
      
      for(int i=0; i< sizeof(arr)/sizeof(int); i++)
      {
              cout<<arr[i]<<" ";
      }
      
                
      cout<<"\n\nEnter a number to search: ";
      cin>>search;
      
      if(binarySearch(arr, search, 0, sizeof(arr)/sizeof(int)-1) != -999 ){
                           
          cout<<"Found(index): "<<binarySearch(arr, search, 0, sizeof(arr)/sizeof(int)-1)<<endl;
      }
      else
      {
          cout<<"Sorry! I cannot find that element"<< endl;
      }
          
      
system("pause");
}

int binarySearch(int arr[], int find, int left, int right)
{
     int mid = (left + right )/2; //
     
     if(left == 0 && right ==0) //bug fix--if searching number is located in the first index
             mid = 0;
             
     if(find == arr[mid])
     {
            return mid;
     } 
     else if(find < arr[mid])
     {
           return binarySearch(arr, find, left, mid - 1);
     }
     else if(find > arr[mid])
     {
          return binarySearch(arr, find, mid + 1 , right);
     }
     else
     {
         return -999;
     }
}


Posted in Uncategorized | Leave a comment

Bubble Sort with C++ with Smart Swapping technique


#include<iostream>

using namespace std;

main()
{
      int arr[] = {5,1,4,2,8};
      int temp;
      
      for(int j=0; j< sizeof(arr)/sizeof(int); j++)
      { 
          for(int i=0; i<sizeof(arr)/sizeof(int); i++)
          {
                  if(arr[i] > arr[i+1])
                  {
                            /*
                            temp = arr[i];
                            arr[i] = arr[i+1];
                            arr[i+1] = temp;
                            */
                            // here's a fun part. Swap two values without using a temp var!
                            arr[i] = arr[i] + arr[i+1];
                            arr[i+1] = arr[i] - arr[i+1];
                            arr[i] = arr[i] - arr[i+1];
                            
                  }
          }
      }
          
      for(int i=0; i<sizeof(arr)/sizeof(int); i++)
      {
             cout<<arr[i]<<endl;
      }
system("pause");
}

              
              

              
              
Posted in Uncategorized | Tagged , | Leave a comment

Simple Telephone Directory using C++


#include<iostream>
#include<string>

using namespace std;

main()
{
      string add[5][2] = { {"128","RM500"}, {"204","RM750"}, {"601","RM495"}, {"609","RM800"}, {"612","RM940"} } ;
      string addnum;
      
      for(int i=0; i<5; i++)
      {
              for(int j=0; j<2; j++)
              {
                     cout<<add[i][j]<<"-" ; 
              }
              cout<<endl;
      }  
      
      start:
            
      cout<<"Enter a address number: ";
      //getline(cin, addnum);
      cin>>addnum;
      
      
      int i=0;
      bool isFound = false;
      
      for( i=0; i<5; i++)
      {
             if(add[i][0] == addnum)
             {
                          isFound = true;
                          break;
             }
      }
      
      if(isFound == true)
      {
                 cout<<"Rent: "<<add[i][1]<<endl;
                   
      }
      
      goto start;
      
system("pause");
      
}
Posted in Uncategorized | Leave a comment

Rounding to Nearest Int, Tenth, Hundredth, Thousandth

#include<iostream>
#include<cmath>


using namespace std;
float num;

float roundToInteger(float x)
{
     return floor( x + .5);
    
}

float roundToTenths(float x)
{
    x /=10;
    return floor(x + 0.5) * 10;
}
    
float roundToHundredths(float x){
      x /=100;
       return floor(x + 0.5) * 100;
}

float roundToThousandths(float x)
{
     x /=1000;
       return floor(x + 0.5) * 1000;  
}

main(){
       cout<<"enter a number: ";
       cin>>num;
       cout<<"\nroundToInteger: " << roundToInteger(num)<<endl;
       cout<<"\nroundToTenths: " << roundToTenths(num)<<endl;
       cout<<"\nroundToHundredths: " << roundToHundredths(num)<<endl;
       cout<<"\nroundToThounsandths: " << roundToThousandths(num)<<endl;
       
system("pause");
  
}

Posted in Uncategorized | Tagged | Leave a comment

Reversing a number

#include<iostream>
#include<string>

/**********Note
fmod(float, float) library function for %
*/
using namespace std;

void getReversed(int);

int num;
int var;
      
main()
{
      
      cout<<"Enter a number: "<<endl;
      cin>>num;  
      getReversed(num);
      
system("pause");

}

void getReversed(int num)
{
     int temp;
     
     while(num > 0)
     {
              // *var = num % 10;
               cout<< num % 10;
               num /=10;
     }             
}
Posted in Uncategorized | Leave a comment