Showing posts with label cplusplus. Show all posts
Showing posts with label cplusplus. Show all posts

Thursday, 19 July 2012

Arrays within a Class in C++

#include<iostream>
#include<string>


/*Let us consider a shopping list of different items for which we place an order with a dealer.This list includes
details such as the code and the price of each item. We would like to perform operations such as adding an item to the list, deleting an item from a list and printing the total value of the order.*/


using namespace std;

const int  val=50;

class ITEM
{
private:
    int item_code[val];
    int item_price[val];
    int count;
public:
    void initiliaze();
    void get_item();
    void display_item();
    void display_sum();
    void remove();
};
void ITEM::initiliaze()
{
    count=0;
}

void ITEM::get_item()
{
    cout<<"Enter the Item code == "<<endl;
    cin>>item_code[count];

    cout<<"Enter the Item cost == "<<endl;
    cin>>item_price[count];
    count++;
}

void ITEM::display_sum()
{
    int sum=0;
    for(int i=0; i<count;i++)
    {
        sum=sum + item_price[i];
    }
    cout<<"The Total Value Of The Cost Is == "<<sum<<endl;
}

void ITEM::display_item()
{
    cout<<"\nCode  Price\n";
    for(int k=0;k<count;k++)
    {
        cout<<"\n"<<item_code[k];
        cout<<"   "<<item_price[k];
    }
}
void ITEM::remove()
{
    int del;
    cout<<"Enter the code you want to remove == ";
    cin>>del;
    for(int search=0; search<count; search++)
    {
        if(del == item_code[search])
        {
            item_price[search]=0;
            item_code[search]=0;
        }
    }
}


int main()
{
    ITEM order;

    order.initiliaze();
    int x;
    do
    {
        cout<<"\n\nYou have the following opton";
        cout<<"\nEnter the Appropriate number";

        cout<<"\n\nPress 1 for ADD AN ITEMS";
        cout<<"\n\nPress 2 for DISPLAY TOTAL VALUE";
        cout<<"\n\nPress 3 for DELETE AN ITEM";
        cout<<"\n\nPress 4 for DISPLAY ALL ITEMS";
        cout<<"\n\nPress 5 for QUIT";

        cout<<"\nEnter The Desired Number == \n";
        cin>>x;
        switch(x)
        {
        case 1:
            {
                order.get_item();
                break;
            }
        case 2:
            {
                order.display_sum();
                break;
            }
        case 3:
            {
                order.remove();
                break;
            }
        case 4:
            {
                order.display_item();
                break;
            }
        case 5:
            break;
        default: cout<<"Incorrect option Please Press the right number == ";
        }

    }while(x!=5);

    getchar();
    return 0;
}

Wednesday, 11 July 2012

Composition in C++


#include<iostream>
#include<string>
using namespace std;

class author_name
{
private: string author;
public: author_name(string auth)
{
author=auth;
}
public: string get_author()
{
return author;
}
public: void show()
{
cout<<"Author Name is == "<<author<<endl;
}
};



class Book
{
private: string book_name;
author_name *myauthor;
public: Book(string aut,string b_name)
{
myauthor=new author_name(aut);
book_name=b_name;
}
public: void show()
{
cout<<"Book name == "<<book_name<<endl;
myauthor->show();
}
};

int main()
{
Book mybook("ABCDEF","C++ LANGUAGE");
mybook.show();
getchar();
return 0;
}

Tuesday, 10 July 2012

Classes in C++

#include<iostream>

using namespace std;

//class
class Gradebook
{
    // Data members Scope public
    public: int  marks1;
    public: int  marks2;

  
   //perametarized Constructor of class
   public: Gradebook(int m1, int m2)
   {
     marks1=m1;
     marks2=m2;
   }
  
  //Member methods or functions
  public: void  Display_Marks()
  {
     cout<<"Marks1 == "<<marks1<<endl;
     cout<<"Marks2 == "<<marks2<<endl;         
  }

};


int main()
{
  Gradebook  mygradebook=  Gradebook(100,23);
  mygradebook.Display_Marks();


getchar();
return  0;
}

Use of Getter-Setter Function in C++

#include<iostream>
using namespace std;
using std::cout;
using std::cin;

#include<string>
using std::string;
using std::getline;



class book
{
private: string Book_Title;
private: int Book_ISBN;
private: string Author_Name;

  //defualt constructor
  public: book()
  {
  }
 
  //Setter Functions
  public: void setbook(string tit, string A_name, int isbn)
  {
      Book_Title=tit;
      Author_Name=A_name;         
      Book_ISBN=isbn;     
  }

  // getter Functions
  public: string getbook_Title()
  {
      return Book_Title;
       
  }

  public: string getbook_Author_Name()
  {
      return Author_Name;
       
  }

  public: int getbook_ISBN()
  {
      return Book_ISBN;
       
  }

};


int main()
{
  book mybook= book();
  string Author_Name;
  string Book_Title;
  int isbn;
  cout<<"Enter the Author Name == ";
  getline(cin,Author_Name);
  cout<<"\n";
  cout<<"Enter the Book Title == ";
  getline(cin,Book_Title);
  cout<<"\n";
  cout<<"Enter the ISBN NO == ";
  cin>>isbn;
  cout<<"\n";



  mybook.setbook(Book_Title,Author_Name,isbn);

  cout<<"Book TITLE == "<<mybook.getbook_Title()<<endl;
  cout<<"Author Name == "<<mybook.getbook_Author_Name()<<endl;
  cout<<"ISBN NO == "<<mybook.getbook_ISBN()<<endl;
    
getchar();

return 0;
}