Program to illustrate access control of inherited members
in the
privately derived class:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int LEN=25;
class Employee
{
private:
char name[LEN];
unsigned long enumb;
public:
void getdata()
{
cout<<”Enter Name=”;
gets(name);
cout<<”Enter Employee Number:”;
cin>>enumb;
}
void putdata()
{
cout<<”Name:”<<name<<”\t”;
cout<<”Emp. Number:”<<enumb<<”\t”;
cout<<”Basic Salary:”<<basic;
}
protected:
float basic;
void getbasic()
{
cout<<”Enter Basic:”;
cin>>basic;
}
};
class Manager:public Employee
{
private:
char title[LEN];
public:
void getdata()
{
Employee::getdata();
getbasic();
cout<<”Enter Title:”;
gets(title);
cout<<”\n”;
}
void putdata()
{
Employee::putdata();
cout<<”\tTitle:”<<title<<”\n”;
}
};
void main()
{
Manager m1,m2;
cout<<”Manager1\n”;
[Link]();
cout<<”Manager2\n”;
[Link]();
cout<<”\t\tManager 1 Details\n”;
[Link]();
cout<<”\t\tManager2 Details\n”;
[Link]();
getch();
}
Program to create a text file and then create another
text file by
converting each line of the newly created text file into
an
uppercase string:
# include <fstream.h>
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main()
{
char fname[20];
char str[80], ch = 'y';
int count;
cout << "\n Enter the name of the file ";
gets(fname);
ofstream oldfile(fname);
while ((ch == 'Y') || (ch == 'y'))
{
cout << "\n Enter a line of text ";
gets(str);
oldfile << str << "\n";
cout << "\n Want to add more lines (Y/N) ";
cin >> ch;
}
[Link]();
// Copy each line after converting into the upper case
ofstream temp("[Link]");
ifstream newfile(fname);
while (newfile)
{
[Link](ch);
if (ch != '\n')
ch = toupper(ch);
[Link](ch);
}
[Link]();
[Link]();
clrscr();
cout<< "\n The output file is \n";
ifstream yfile("[Link]");
count = 0;
while (![Link]())
{
[Link](str,80);
cout << str << "\n";
count++;
// Check whether the number of lines on the display exceed 22 or
not
if ((count % 22) == 0)
{
cout << "\n Press any key to continue ";
cin >> ch;
}
}
[Link]();
}
Addition and deletion of new books using linked list:
# include<iostream.h>
# include <conio.h>
class NODE
{
public:
int bookno;
NODE *link;
};
class LINKLIST
{
private:
NODE *first,*last,*temp;
public:
void NCREATE(int);
// Create a linked list with n nodes
void insertatk(int); // Inserting at kth position
void display();
// Display the linked list
};
void LINKLIST::NCREATE( int n)
{
first = new NODE;
cout << "\n Enter the book number ";
cin >> first->bookno;
first->link = NULL;
temp = first;
for(int i =1;i<n;i++)
{
last = new NODE;
cout << "\n Enter the book number ";
cin >> last->bookno;
last->link = NULL;
temp->link = last;
temp = last;
}
}
void LINKLIST::insertatk(int j) // Function to insert the node at kth
position
{
int i = 0;
NODE *newnode,*back;
newnode = new NODE;
cout<< "\nEnter the data value ";
cin>>newnode->bookno;
newnode->link = NULL;
temp = first;
while (i < (j-1))
{
back = temp;
temp = temp->link;
i++;
}
back->link = newnode;
newnode->link = temp;
}
void LINKLIST::display() //Function to display the link list
{
temp = first;
cout<< "\n The linked list is \n";
while (temp != NULL)
{
cout<< "\n"<<temp->bookno;
temp = temp->link;
}
getch();
}
void main()
{
int ch,n,k;
char ch1 = 'y';
LINKLIST list;
clrscr();
cout << "\n Enter how many nodes in the list ";
cin >> n;
[Link](n);
do
{
clrscr();
cout<< "\n1. For insert ";
cout << "\n2. For display ";
cout << "\n3. For quit ";
cout << "\nEnter your choice ";
cin>>ch;
switch (ch)
{
case 1:
cout << "\nEnter the position at which insertion is
required ";
cin >> k;
[Link](k);
break;
case 2 :
[Link]();
break;
}
} while (ch != 3);
}
Program to create two arrays to store roll numbers and marks
of
desired students, whose number would be known at run time:
#include<iostream.h>
#include<conio.h>
int*rollno;
float*marks;
void main()
{
clrscr();
int size;
cout<<"How many elements are there in the array?\n";
cin>>size;
rollno=new int[size];
marks=new float[size];
if((!rollno) || (!marks))
{
cout<<"Out of memory! Aborting!";
return;
}
for(int i=0;i<size;i++)
{
cout<<"Enter rollno and marks for student"<<(i+1)<<"\n";
cin>>rollno[i]>>marks[i];
}
cout<<"Roll no \tMarks\n";
for(i=0;i<size;i++)
cout<<"\t"<<rollno[i]<<"\t\t"<<marks[i]<<"\n";
delete[]rollno;
delete[]marks;
getch();
}
Program to check whether the string is palindrome or not:
#include<iostream.h>
#include<string.h>
#include<conio.h>
//CLASS DECLARATION
class strn
{
char *a, flag;
int i, j, k;
public:
void read();
//MEMBER FUNCTIONS
void ch_pal();
strn()
//USE OF CONSTRUCTOR
{
flag = 'y';
}
};
//END OF CLASS
void strn::read()
{
cout << "\n\t";
cout << "\n\tEnter the string ";
cin >> a;
//TO READ THE STRING
}
void strn::ch_pal()
{
cout << "\n\tThe entered string ";
for(i=0; *(a+i)!='\0'; i++)
cout << *(a+i);
for(j=0, i-=1; i>j; j++, i--)
{
if(*(a + i) != *(a+j))
//CHECKING FOR PALINDROME
{
flag = 'n';
break;
}
}
if(flag == 'n')
cout << " is not a palindrome ";
else
cout << " is a palindrome ";
}
void main()
{
strn x;
//DECLARATION OF OBJECT
clrscr();
cout << "\n\n\n\t ";
[Link]();
//CALLING MEMBER FUNCTIONS
x.ch_pal();
cout << "\n\n\t\t bye!";
getch();
}