Thursday, 4 May 2017

AIM:Write a C++ program to implement following types of Inheritances:1) Hybrid Inheritances.2) Multilevel Inheritance.

 CODING:
//Hybrid Inheritance.
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
};
class B: public A
{
public:
int b;
};
class D
{
public:
int d;
};
class C: public B,public D
{
public:
void add()
{
a=2,b=3,d=5;
int c=a+b+d;
cout<<"Addition is ="<<c;
}
};
main()
{
clrscr();
C c;
void add();
getch();
return(0);

}

AIM:Write a C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file





CODING:
#include<fstream.h>
#include<conio.h>
class student
{
   char name[20];
   int roll;
   public:
     void getdata()
     {  cout<<"Enter Name:"<<endl;
            cin>>name;
            cout<<"Enter roll No:"<<endl;
            cin>>roll;
     }
       void showdata()
       { cout<<"Name is:"<<name<<endl;
             cout<<"Roll No is"<<roll<<endl;
            }
};
main()
{  clrscr();
   student s[2];

   ofstream of;
   of.open("student.txt");
   for(int i=0;i<2;i++)
   {
     s[i].getdata();
     of.write((char *)&s[i],sizeof(s[i]));
     of.close();
   }
   ifstream fin;
   fin.open("student.txt");
   for(i=0;i<2;i++)
   {
     fin.read((char *)&s[i],sizeof(s[i]));
     s[i].showdata();
     fin.close();
   }
   getch();
   return 0;

}

AIM: Define an array which stores arr[]={11,22,33,44,55,66,77,88,99}. Write a program to illustrate the use of command line arguments that supplies the file name called ODD & EVEN to the program. Using command line arguments the odd numbers’ are stored into the ODD file & even numbers’ are stored into the EVEN file. Finally display the contents from both the files.


  
CODING:
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
 main(int argc,char *argv[])
{
int arr[9]={11,22,33,44,55,66,77,88,99};
if(argc!=3)
{
cout<<"parameter is not match:";
exit(1);
}
ofstream f1,f2;
f1.open(argv[1]);
if(f1.fail())
{
cout<<"error in opening file";
exit(1);
}
for(int i=0;i<=9;i++)
{
if(arr[i]%2==0)
{
f1<<arr[i];
}
f1.close();
f2.close();
ifstream fin;
char ch;
for(i=0;i<=argc;i++)
{
fin.open(argv[i]);
cout<<"content of file are";
do
{
fin.get(ch);
}
while(fin.eof());
cout<<"\n";
}
fin.close();
}
if(!argc==3)
{
cout<<"array elements are"<<endl;
cin>>argc;
}
getch();
return(0);

}

AIM: Write a C++ program that counts number of words in a file.


CODING:
#include<fstream.h>
#include<conio.h>
main()
{
clrscr();
int count=1;
ifstream f1;
f1.open("student.txt");
f1.seekg(0);
char ch;
while(! f1.eof())
{
 f1.get(ch);
 }
 if(ch==' '|| ch=='\t'|| ch=='\n')
 {
  count++;
 }
 cout<<"Number of words="<<count;
 f1.close();
 getch();
 return 0;

}

C++ Programs for Stack Implementation, Array of Objects, Friend Function

Today I am providing Various Important C++ Programs with coding.
AIM:
Write a C++ program to implement a stack with its constructor and two member functions PUSH and POP.

CODING:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#define size 4
class stact
{
int st;
int tos;
public:
stact();
~stact();
void push(int x);
pop ();
};
void stact::st()
{
tos=0;
cout<<"stact incremented"<<endl;
}
void stact::~stact()
}
  cout<<"stact is empty"<<endl;
  return ;
}
void stact::push(int x)
{
if(tos==size)
{
cout<<"stack is full"<<endl;
return;

}
stack[tos]=x;
tos++ ;

};
void st::pop()
{
if(t==0)
{
cout<<"stack is empty"<<endl;
}---;
return stack[t];
}
void main()
{
st s1,s2,s3,s4;
s1.push(1);
s2.push(2);
s3.push(3);
s4.push(4);
cout<<" "<<s4.pop();
cout<<" "<<s3.pop();
cout<<" "<<s2.pop();
cout<<" "<<s1.pop();
getch();

}
----------------------------------------------------------------------------------------------------------
AIM:
Write a C++ program for an array of objects.

CODING:

#include<iostream.h>
#include<conio.h>
class Student
{
  public:
            char name[20];
            int enrol;
            char section[20];
            void getdata()
            {
              cout<<"Enter the Name,Enrol & Section"<<endl;
              cin>>name>>enrol>>section;
            }
            void showdata()
            {
              cout<<"Name of Student is : "<<name<<endl;
              cout<<"Enrolment no. is : "<<enrol<<endl;
              cout<<"Section is : "<<section<<endl;
            }
};
main()
{
   Student s[3];
   for(int i=0;i<3;i++)
   {
     s[i].getdata();
   }
   for(i=0;i<3;i++)
   {
     s[i].showdata();
   }
   getch();
   return 0;
}
----------------------------------------------------------------------------------------------------------
AIM:
Write a C++ program for Friend function.


CODING:
#include<iostream.h>
#include<conio.h>
class demo
{
  private:
  int a,b;
  public:
  void setvalue()
  {
    a=10;
    b=15;
  }
   friend int add(demo d);
};
 int add(demo d)
 {
   return int(d.a+d.b);
 }
 main()
 {
  clrscr();
   demo d1;
   d1.setvalue();
   cout<<"addition is="<<add(d1);
   getch();
   return 0;
 }

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.