Showing posts with label CS6301 Programming and Data Structure II. Show all posts
Showing posts with label CS6301 Programming and Data Structure II. Show all posts

Saturday, February 13, 2016

FUNCTION TEMPLATE

AIM:
         To implement a C++ program to swap the numbers using the concept of function template.

ALGORITHM:

Step 1:  Include the header files
Step 2:  Declare the template class.
Step 3:  Declare and define the functions to swap the values.
Step 4:  Declare and define the functions to get the values.
Step 5:  Read the values and call the corresponding functions.
Step 6:   Display the results.
PROGRAM:                 
#include<iostream.h>
#include<conio.h>
template<class t>
 void swap(t &x,t &y)
{
   t temp=x;
   x=y;
   y=temp;
}
void fun(int a,int b,float c,float d)
{
   cout<<"\na and b before swaping :"<<a<<"\t"<<b;
   swap(a,b);
   cout<<"\na and b after swaping  :"<<a<<"\t"<<b;
   cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
   swap(c,d);
   cout<<"\nc and d after swaping  :"<<c<<"\t"<<d;
}
void main()
{
    int a,b;
    float c,d;
    clrscr();
    cout<<"Enter A,B values(integer):";
    cin>>a>>b;

    cout<<"Enter C,D values(float):";

Sunday, January 24, 2016

COPY CONSTRUCTOR

AIM:
         To implement a C++ program to calculate factorial of a given number using copy constructor.
ALGORITHM:
Step 1:  Include the header files
Step 2:  Declare the class name as copy with data members and member functions.
Step 3:  The constructor copy () with argument to assign the value.
Step 4:  To cal the function calculate () do the following steps.
Step 5:   for i=1 to var do
Step 6:   Calculate fact*i to assign to fact.
Step 7:   Increment the value as 1.
Step 8:   Return the value fact.
Step 9:   Print the result.
.
PROGRAM:

#include<iostream.h>
#include<conio.h>
class copy
{
              int var,fact;
              public:

                copy(int temp)
                {
                 var = temp;
                }

                double calculate()
                {
                            fact=1;
                            for(int i=1;i<=var;i++)
                            {
                            fact = fact * i;
                            }
                            return fact;                           
                }
};
void main()
{
    clrscr();
    int n;
    cout<<"\n\tEnter the Number : ";
    cin>>n;
    copy obj(n);
    copy cpy=obj;
    cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
    cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
    getch();
}  

 SAMPLE OUTPUT:

Enter the Number: 5
Factorial is: 120
Factorial is: 120

Saturday, January 23, 2016

DESTRUCTOR

AIM:
         To implement a C++ program to print student details using constructor and destructor
ALGORITHM:
Step 1: Include the header files
Step 2: Invoke The Classes
Step 3: Call The Read() Function
Step 4: Get The Inputs Name ,Roll Number And Address
Step 5: Call The Display() Function
Step 6: Display The Name, Roll Number, And Address Of The Student

PROGRAM:
#include<iostream.h>
#include<conio.h>
class stu
{
private: char name[20],add[20];
  int roll,zip;
public: stu ( );//Constructor
~stu( );//Destructor
void read( );
void disp( );
};
stu :: stu( )
{
cout<<”This is Student Details”<<endl;
}
void stu :: read( )
{
cout<<”Enter the student Name”;
cin>>name;
cout<<”Enter the student roll no “;
cin>>roll;
cout<<”Enter the student address”;
cin>>add;
cout<<”Enter the Zipcode”;
cin>>zip;
}
void stu :: disp( )
{
cout<<”Student Name :”<<name<<endl;
cout<<”Roll no   is       :”<<roll<<endl;
cout<<”Address is       :”<<add<<endl;
cout<<”Zipcode is       :”<<zip;
}
stu : : ~stu( )
{
cout<<”Student Detail is Closed”;
}
void main( )
{
stu s;
clrscr( );
s.read ( );
s.disp ( );
getch( );
}
SAMPLE OUTPUT:

Enter the student Name
James
Enter the student roll no
01
Enter the student address
Newyork
Enter the Zip code
919108
Student Name: James
Roll no is : 01
Address is : Newyork
Zip code is :919108

Friday, January 22, 2016

CONSTRUCTOR

AIM:
To implement a C++ program to verify whether a give number is  prime number or not  using Constructor.
ALGORITHM:
Step 1:  Include the header files
Step 2:  Declare the class as Prime with data members and Member functions.
Step 3:  Consider the argument constructor Prime () with integer Argument.
Step 4:  To cal the function calculate () and do the following steps.
Step 5:  For i=2 to a/2 do
Step 6:  Check if a%i==0 then set k=0 and break.
Step 7:  Else set k value as 1.
Step 8:  Increment the value i as 1.
Step 9:  Check whether the k value is 1 or 0.
Step 10: If it is 1 then display the value is a prime number.
Step 11: Else display the value is not prime.

PROGRAM:
#include<iostream.h>
#include<conio.h>
using namespace std;
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}

void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<"\n"<<a<<" is Prime Number.";
else
cout<<"\n"<<a<<" is Not Prime Numbers.";
}
};

Thursday, January 21, 2016

Generic programming

Generic programming means that you are not writing source code that is compiled as-is but that you write "templates" of source codes that the compiler in the process of compilation transforms into source codes. The simplest example for generic programming are container classes like arrays, lists or maps that contain a collection of other objects. But there's much more to generic programming. In the context of C++ .it means to write programs that are evaluated at compile time. A basic example of generic programming are templates of containers: In a statically typed language like C++ you would have to declare separate containers that hold integers, floats, and other types or deal with pointers to void and therefore losing all type information. Templates which are the C++ way of generic programming leverage this constraint by letting you define classes where one or more parameters are unspecified at the time you define the class. When you instance the template later you tell the compiler which type it should use to create the class out of the template. 

Example: template class MyContainer { // Container that deals with an arbitrary type T }; 

void main() 
{
 // Make MyContainer take just
 ints.  MyContainer intContainer;
 }

Wednesday, January 20, 2016

SIMPLE C++ PROGRAM

AIM:

To implement a simple C++ program to display the name and number
ALGORITHM:
Step 1:  Include the header files
Step 2:  Declare the class as person with data members name and number
Step 3:  Create an object for the class person
Step 4:  Get the name and number as input
Step 5:  Access the data member using object and display the output.
               
PROGRAM:
#include <iostream.h>
#include<conio.h>
using namespace std;
class person
{
public:
 string name;
  int number;
 void accept()
{
cin>>name>>number;
}
 void display()
{
cout>>name>>number;
}
};
int main()
{
  person obj;
  obj.accept();
obj.display();
  getch();
  return 0;
}

SAMPLE OUTPUT:
Enter the Name: SAHANA
Enter the Number: 100
SAHANA: 100

 RESULT:
Thus the simple C++ program to display the name and number is implemented successfully.