Sunday, October 30, 2016

Simple Program Book Entry Using structure Variable in C++ Programming

Example Program

#include<iostream.h>
#include<stdio.h>

struct books
{
    char name[20],author[20];
}a[50];
int main()
{
    
    int i,n;
    cout<<"No Of Books[less than 50]:";
    cin>>n;
cout<<"Enter the book details\n";
cout<<"----------------------\n";
    
    for(i=0;i<n;i++)
    {
 cout<<"Details of Book No "<<i+1<<"\n";
 cout<<"Book Name :";
 cin>>a[i].name;
 cout<<"Book Author :";
 cin>>a[i].author;
 cout<<"----------------------\n";
    }
    cout<<"================================================\n";
    cout<<" S.No\t| Book Name\t|author\n";
    cout<<"=====================================================";
    for(i=0;i<n;i++)
    {
 cout<<"\n  "<<i+1<<"\t|"<<a[i].name<<"\t| "<<a[i].author;
    }
    cout<<"\n=================================================";
    
    return 0;
}



Sample Output

No Of Books[less than 50]:2
Enter the book details
----------------------
Details of Book No 1
Book Name :Programming
Book Author :Dromy
Details of Book No 2
Book Name :C
Book Author :Byron
=======================================================
 S.No | Book Name |author
=======================================================
  1 |Programming | Dromy
  2 |C | Byron
=======================================================

Simple Program for Static Data and Member Function Using C++ Programming

 To count the object value using the storage keyword static.

ALGORITHM: 

STEP 1:  Start the program.

STEP 2:  Declare the class name as Stat with data member s and member functions.
STEP 3:  The constructor Stat() which is used to increment the value of count as 1 to to assign the variable code.
STEP 4:  The function showcode() to display the code value.
STEP 5:  The function showcount() to display the count value.

STEP 6:   Stop the program.



PROGRAM: by abglobe.blogspot.com

#include<iostream.h>
#include<conio.h>
class stat
{
    int code;
    static int count;
   public:
    stat()
    {
      code=++count;
    }
    void showcode()
    {
      cout<<"\n\tObject number is :"<<code;
    }
    static void showcount()
    {
              cout<<"\n\tCount Objects :"<<count;
    }
};

int stat::count;
void main()
{
   clrscr();

   stat obj1,obj2;
   obj1.showcount();

   obj1.showcode();

   obj2.showcount();

   obj2.showcode();

   getch();
}

Output: 

Count Objects: 2

Object Number is: 1
Count Objects: 2
Object Number is: 2

thanks for watching!!!!!!

any problem please comment @

Simple Program for Friend Function Using C++ Programming

To find the mean value of a given number using friend function.

ALGORITHM:

STEP 1:  Start the program.
STEP 2:  Declare the class name as Base with data members and member functions.
STEP 3:  The function get() is used to read the 2 inputs from the user.
STEP 4:  Declare the friend function mean(base ob) inside the class.
STEP 5:  Outside the class to define the friend function and do the following.
STEP 6:  Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7:  Stop the program.



#include<iostream.h>

#include<conio.h>

class  base

{
    int val1,val2;

   public:

    void get()
    {
       cout<<"Enter two values:";

       cin>>val1>>val2;
    }
    friend float mean(base ob);
};

float mean(base ob)
{
   return float(ob.val1+ob.val2)/2;
}

void main()

{
    clrscr();

    base obj;

    obj.get();

    cout<<"\n Mean value is : "<<mean(obj);

    getch();
}      


Output:

Enter two values: 10, 20
Mean Value is: 15

thanks for watching!!!!!!

any problem please comment @

    

Simple Program for Binary Operator Overloading Using C++ Programming

To write a program to add two complex numbers using binary operator overloading.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and     operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class complex
 {
    int a,b;

     public:

      void getvalue()
        {

          cout<<"Enter the value of Complex Numbers a,b:";

          cin>>a>>b;

        }

          complex operator+(complex ob)

           {

              complex t;

              t.a=a+ob.a;

              t.b=b+ob.b;

              return(t);
            }

              complex operator-(complex ob)

              {

                 complex t;

                 t.a=a-ob.a;

                 t.b=b-ob.b;

                 return(t);
              }

                void display()

              {

                            cout<<a<<"+"<<b<<"i"<<"\n";

              }
 };

    void main()

     {

       clrscr();

       complex obj1,obj2,result,result1;
       obj1.getvalue();

       obj2.getvalue();
       result = obj1+obj2;

       result1=obj1-obj2;
       cout<<"Input Values:\n";

       obj1.display();

       obj2.display();
  
       cout<<"Result:";

       result.display();

       result1.display();
  
       getch();
     }

Output:


Enter the value of Complex Numbers a, b
4                  5

Enter the value of Complex Numbers a, b
2                  2

Input Values
4 + 5i
2 + 2i

Result
6 +   7i
2 +   3i

thanks for watching!!!!!!

any problem please comment @

CONSTRUCTORS AND TYPES


C++ Provides a special member function called constructor that enables an object to initialize itself when it is created.This is know as automatic initialization of objects.

it special function because its name is same as the class name.The constructor is invoked whenever an object of its associated class is created.
A Constructor is declared and defined as follows :


                                                      class student
                                                       {
                                                       private:
                                                                   int roll no;
                                                                   int marks;
                                                       public:
                                                                    student( )             // default constructor defined
                                                                     {
                                                                             roll no=0;
                                                                             marks=0;
                                                                      }
                                                        };
                                                         void main( )
                                                          {
                                                                 student s;                  // object s created
                                                           }
                                                   

                         in this example, the declaration
                                                                             
                                                                               student s;
 not only created the object s of type student but also initialize its data members roll no,marks to zero.

characteristics of a constructor


* A constructor name must be the same as that of its class name.
* it is declared with no return type(not even void)
* it is declared in public section of the class.
* it can have default arguments.
* it can not refer to its own address.
* it may not be static.
* it may not be virtual.
* it can not be inherited.


types of constructors

there are three types of constructors 


1. default constructors

2. parameterized constructors

3. copy constructors

1.  Default Constructor:-  Default Constructor is also called as Empty Constructor which has no arguments and It  is Automatically called when we creates the object of class  but Remember name of Constructor is same as name of class and Constructor never declared with the help of Return Type. Means we cant Declare a Constructor with the help of void Return Type. , if we never Pass or Declare any Arguments then this called as the Copy Constructor.

/*  Example Program For Simple Example Program Of default Constructor In C++

 #include <iostream.h>
 #include<conio.h>
       class Defal
           {
            public:

            int x;    // Variable Declaration

            int y;

            Defal(){x=y=0;}    //default constructor
                           
          };

          int main()
           {
           Defal A;

            cout << "Default constructs x,y value::"<<

            A.x <<" , "<< A.y << "\n";

            return 0;
         }

2.  Parameterized Constructor :- This is Another type  Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.

/*  Example Program For Simple Example Program Of Parameterized Constructor In C++   

    Coded By:abglobe.blogspot.com             */

#include<iostream>
#include<conio.h>

using namespace std;

class Example        {
   
int a,b;  // Variable Declaration
    public:

    
    Example(int x,int y)  //Constructor           
    {
    
    a=x;       // Assign Values In Constructor

    b=y;

    cout<<"Im Constructor\n";
    }

    void Display()
    {

    cout<<"Values :"<<a<<"\t"<<b;
    
    }

  };

       int main()        
        {

           Example Object(10,20);
        
           Object.Display();   // Constructor invoked

           getch();  // Wait For Output Screen
           
           return 0;
         
         }


3.  Copy Constructor:- This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & Ampersand or Address Operator.

/*  Example Program For Simple Example Program Of Copy Constructor Overloading In C++

    Coded By:abglobe.blogspot.com             */

#include<iostream>
#include<conio.h>

using namespace std;

class Example        
{
    
    int a,b;  // Variable Declaration
    public:

    Example(int x,int y)   //Constructor with Argument
     {
    
    a=x;     // Assign Values In Constructor

    b=y;

    cout<<"\nIm Constructor";

    }

    void Display() 
    {

    cout<<"\nValues :"<<a<<"\t"<<b;

    }
 };

int main() 
               {
    Example Object(10,20);

       
        Example Object2=Object;   //Copy Constructor

        

        Object.Display();     // Constructor invoked.

        Object2.Display();

        getch();   // Wait For Output Screen

        return 0;
}

thanks for watching!!!!!!

any problem please comment @