Tuesday, 8 October 2024

Journal Program 20

 /*

Definition : Construct a class Month with data members as Month

number.Write down overloaded operator++ for the

increment the Month number by one.

Write a member function read() and display() to read

a number of Month and print a number of Month.

Input:

Enter Month number : 8

Output:

Next Month number is : 9

Input:

Enter Month number : 12

Output:

Next Month number is : 1


*/

#include<iostream.h>

#include<conio.h>

class Month

{

int mno;

public:

void getData()

{

cout<<"\nEnter valid month number : ";

cin>>mno;

}

void operator ++()

{

mno++;

if(mno>12)

mno=1;

}

void putData()

{

cout<<"\nNext month is : "<<mno;

}

};

int main()

{

clrscr();

Month m;

m.getData();

m.operator ++(); //m++ or ++m

m.putData();

getch();

return 0;

}


Journal Program 5

 /*

Def: WAP to build a class MyArray having integer array as

a data member and following member fuctions:

(a)getData() : To get n values for integer array

(b)sort_asc() : To sort an array in ascending order

(c)sort_desc() : To sort an array in descending order

(d)putData() : To print all values

Write a menu driven program for sorting either ascending

and descending order and also for exit from a program.

Tut Date:

Lab Date:


*/

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class MyArray

{

int a[10];

int i,j,n,temp;

public:

void getData();

void sort_asc();

void sort_des();

void putData();

};

void MyArray::getData()

{

cout<<"Enter no of total elements(Maximum 10) : ";

cin>>n;


for(i=0;i<n;i++)

{

cout<<"a["<<i<<"]=";

cin>>a[i];

}

}

void MyArray::sort_asc()

{

for(i=0;i<n;i++)

{

for(j=0;j<n-1;j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

}

void MyArray::sort_des()

{

for(i=0;i<n;i++)

{

for(j=0;j<n-1;j++)

{

if(a[j]<a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}


}

void MyArray::putData()

{

for(i=0;i<n;i++)

{

cout<<"\n a["<<i<<"]="<<a[i];

}

}


int main()

{

clrscr();

MyArray m;

int choice;

char ch;

m.getData();

do

{

cout<<"\n 1:Ascending Order";

cout<<"\n 2:Descending Order";

cout<<"\n 3:Exit";

cout<<"\n Enter your choice=";

cin>>choice;

switch(choice)

{

       case 1:

m.sort_asc();

m.putData();

break;

       case 2:

m.sort_des();

m.putData();

break;

       case 3:

exit(0);

break;

default:

cout<<"\n Invalid choice";

}

cout<<"\n Do you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}

Journal PRogram 4

 /*

Def : WAP to build a class Student,which accept student number,

  name and marks of 3 subjects.Calculate total and

  percentage using different member fuction and display

  all information.

Tut Date:

Lab Date:

*/

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Student

{

int rno,m1,m2,m3,total;

float per;

char name[20];

public :

void getData()

{

cout<<"\n Enter roll number : ";

cin>>rno;

cout<<"\n Enter student name : ";

flushall();

gets(name);

cout<<"\n Enter marks of subject1 : ";

cin>>m1;

cout<<"\n Enter marks of subject2 : ";

cin>>m2;

cout<<"\n Enter marks of subject3 : ";

cin>>m3;

}

void calculate();

void printData();

};

void Student::calculate()

{

total=m1+m2+m3;

per=total/3.0;

}

void Student::printData()

{

cout<<"\n\t\t STUDENT DETAILS ";

cout<<"\n======================================";

cout<<"\n Roll Number : "<<rno;

cout<<"\n Student Name : "<<name;

cout<<"\n Marks of Subject1 : "<<m1;

cout<<"\n Marks of Subject2 : "<<m2;

cout<<"\n Marks of Subject3 : "<<m3;

cout<<"\n Total Marks : "<<total;

cout.precision(2);

cout<<"\n Percentage : "<<per;

cout<<"\n======================================";

}

int main()

{

clrscr();

Student s;

s.getData();

s.calculate();

s.printData();

getch();

return 0;

}


Journal Program 3

 /*

Def : WAP to build a class Person,which contain person name,age

  and city as data members and getData() and printData()

  as memeber functions to read and print person data.

Tut Date:

Lab Date:

*/

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Person

{

int age;

char name[20],city[20];

public :

void getData()

{

cout<<"\n Enter person name : ";

flushall();

gets(name);

cout<<"\n Enter person age : ";

cin>>age;

cout<<"\n Enter city name : ";

flushall();

gets(city);

}

void printData();

};

void Person::printData()

{

cout<<"\n\t\t PERSON DETAILS ";

cout<<"\n======================================";

cout<<"\n Person Name : "<<name;

cout<<"\n Person Age : "<<age;

cout<<"\n City Name : "<<city;

cout<<"\n======================================";


}


int main()

{

clrscr();

Person p;

p.getData();

p.printData();

getch();

return 0;

}


WAP to create a class Employee and declare empno,name, salary and city as data memebers.

 /*

Def : WAP to create a class Employee and declare empno,name,

salary and city as data memebers.Accept data from user and

print them for n employees using concept of Array of objects.

Tut. Date : 

Lab Date :

*/


#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class Employee

{

private:

int empno,salary;   // Data memebers

char name[20],city[20];

public:

void getData();  // member functions

void putData();

};

void Employee:: getData()

{

cout<<"\n Enter employee number : ";

cin>>empno;


cout<<"\n Enter employee name : ";

flushall();

gets(name);


cout<<"\n Enter employee salary : ";

cin>>salary;


cout<<"\n Enter employee city : ";

flushall();

gets(city);

}


void Employee::putData()

{

cout<<"\n Employee Number : "<<empno;

cout<<"\n Employee Name : "<<name;

cout<<"\n Employee Salary : "<<salary;

cout<<"\n Employee City : "<<city;

}


int  main()

{

int n,i;

clrscr();

Employee emp[10];

cout<<"\n Enter the no of employee (Maximum 10) : ";

cin>>n;

for(i=0;i<n;i++)

{

cout<<"\n Enter employee "<<(i+1)<<" details : ";

cout<<"\n==========================================";

emp[i].getData();

cout<<"\n==========================================";

}

cout<<"\n\n";

for(i=0;i<n;i++)

{

cout<<"\n Employee "<<(i+1)<<" details : ";

cout<<"\n==========================================";

emp[i].putData();

cout<<"\n==========================================";

}

getch();

return 0;

}

JOurnal - 19

 /*

Definition :Write a C++ program for overloading

a function volume() to calculate the

volume of cube, cylinder and a rectangular

box. Where volume of

Cube=a*a*a

Cylinder= 3.14*r*h

Rectangular box=l*b*h

Tut. Date :

Lab  Date :


*/

#include<iostream.h>

#include<conio.h>

class Vol

{

public:

void volume(int a)

{

cout<<endl<<"Volume of Cube : "<<a*a*a;

}

void volume(int r,int h)

{

cout<<endl<<"Volume of Cylinder : "<<3.14*r*h;

}

void volume(int l,int b, int h)

{

cout<<endl<<"Volume of Rectangle : "<<l*b*h;

}

};

int main()

{

clrscr();

Vol v;

int a,r,l,b,h;

cout<<"Enter a : ";

cin>>a;

cout<<"Enter r : ";

cin>>r;

cout<<"Enter h : ";

cin>>h;

cout<<"Enter l : ";

cin>>l;

cout<<"Enter b : ";

cin>>b;


v.volume(a);

v.volume(r,h);

v.volume(l,b,h);

getch();

return 0;

}


Build a class String. Use overloaded operator to combine two string.

 /*

Definition : Build a class String. Use overloaded operator + to

combine two string.

*/

#include<iostream.h>

#include<conio.h>

#include<string.h>

class String

{

char str[100];

public:

void getData()

{

cout<<"\n Enter String : ";

cin>>str;

}

void operator +(String s2)

{

int i,len1,len2;

len1=strlen(str);

len2=strlen(s2.str);


for(i=0;i<len2;i++)

{

str[len1]=s2.str[i];

len1=len1+1;

}

str[len1]='\0';

}

void putdata()

{

cout<<"\n Concated String is : "<<str;

}

};

int main()

{

clrscr();

String s1,s2;

s1.getData();

s2.getData();

s1+s2; //s2+s1; or s1.operator +(s2);

s1.putdata();

getch();

return 0;

}

Construct a class Time with data members as hours, minute and second. Implement constructors, overloaded constructors and destructors for the same.

 /*

Definition : Construct a class Time with data members as hours, minute and

second. Implement constructors, overloaded constructors and destructors for

the same. Implement a overloaded operator ++ which will increment the time

by one second. Write a function display() to print hours, minute and second.

Assume that 24 hours clock.


Tutorial Date :

Lab Date :

*/

#include<iostream.h>

#include<conio.h>

class Time

{

int hh,mm,ss;

public:

Time()

{

hh=0;

mm=0;

ss=0;

}

Time(int h,int m,int s)

{

hh=h;

mm=m;

ss=s;

}

void operator ++()

{

ss++;

if(ss>=60)

{

mm=mm+(ss/60);

ss=ss%60;

}

if(mm>=60)

{

hh=hh+(mm/60);

mm=mm%60;

}

if(hh>=24)

{

hh=0;

mm=0;

ss=0;

}

cout<<hh<<":"<<mm<<":"<<ss;


}

~Time()

{

hh=NULL;

mm=NULL;

ss=NULL;

}

};

int main()

{

int h,m,s;

clrscr();

Time t;

cout<<"Enter hour:";

cin>>h;

cout<<"Enter minute:";

cin>>m;

cout<<"Enter second:";

cin>>s;

t=Time(h,m,s);

t.operator++();//t++

getch();

return 0;

}






Construct a class Calendar with data members as date, month and year. Implement constructors, overloaded constructors and destructors for the same.

 /*

Definition : Construct a class Calendar with data members as date, month

and year. Implement constructors, overloaded constructors and destructors

for the same. Write down overloaded operator ++ that will increment the

date by one and print new date, month and year.

e.g. if date is 31/7/2005 then output : tomorrow is 1/8/2005


Tutorial Date :

Lab Date :

*/



#include<iostream.h>

#include<conio.h>

class Calendar

{

int dd,mm,yy;

public:

Calendar()

{

dd=0;

mm=0;

yy=0;

}

Calendar(int d,int m,int y)

{

dd=d;

mm=m;

yy=y;

}

void operator ++()

{

dd++;

if(dd>31)

{

mm++;

dd=1;

}

if(mm>12)

{

yy++;

mm=1;

}

cout<<dd<<"/"<<mm<<"/"<<yy;


}

~Calendar()

{

dd=NULL;

mm=NULL;

yy=NULL;

}

};

int main()

{

int d,m,y;

clrscr();

Calendar c;

cout<<"Enter date:";

cin>>d;

cout<<"Enter month:";

cin>>m;

cout<<"Enter year:";

cin>>y;

c=Calendar(d,m,y);

c.operator++();//c++;

getch();

return 0;

}






Monday, 30 September 2024

Journal Program 13

 #include<iostream.h>

#include<conio.h>

class Cricketer

{

int age,tmatch,trun,twicket;

char name[20],country[20],type[10];

public:

void getData()

{

cout<<"Enter name=";

cin>>name;

cout<<"Enter age=";

cin>>age;

cout<<"Enter country=";

cin>>country;

cout<<"Enter type(Bat/Bow)=";

cin>>type;

cout<<"Enter total matches=";

cin>>tmatch;

cout<<"Enter total runs=";

cin>>trun;

cout<<"Enter total wickets=";

cin>>twicket;

}

void putData()

{

cout<<endl<<"Name="<<name;

cout<<endl<<"Age="<<age;

cout<<endl<<"Country="<<country;

cout<<endl<<"Type="<<type;

cout<<endl<<"Total Matches="<<tmatch;

cout<<endl<<"Total Runs="<<trun;

cout<<endl<<"Total Wickets="<<twicket;

}

};

int main()

{

clrscr();

Cricketer c[5];

int i;

cout<<"Enter total cricketers = ";

cin>>n;

for(i=0;i<n;i++)

{

c[i].getData();

}

for(i=0;i<n;i++)

{

c[i].putData();

}

getch();

return 0;

}




Journal Program 11

 #include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class Factorial

{

int i,fact,n;

public:

Factorial()

{

cout<<"\n Enter value of n : ";

cin>>n;

}

void for_fact()

{

fact=1;

for(i=1;i<=n;i++)

{

fact=fact * i;

}

cout<<"\n "<<n<<"! = "<<fact;

}

void do_fact()

{

fact=1;

i=1;

do

{

   fact = fact * i;

   i++;

}while(i<=n);

cout<<"\n "<<n<<"! = "<<fact;

}


void while_fact()

{

fact=1;

i=1;

while(i<=n)

{

fact=fact*i;

i++;

}

cout<<"\n "<<n<<"! = "<<fact;

}

};

int main()

{

clrscr();

int choice;

char ch;

Factorial obj1;

do

{

cout<<endl<<"1:Factorial using For Loop ";

cout<<endl<<"2:Factorial using Do..While Loop ";

cout<<endl<<"3:Factorial using While Loop ";

cout<<endl<<"4:Exit";


cout<<endl<<"\nEnter your choice=";

cin>>choice;

switch(choice)

{

case 1:

obj1.for_fact();

break;

case 2:

obj1.do_fact();

break;

case 3:

obj1.while_fact();

break;


case 4:

exit(0);

break;

default:

cout<<endl<<"Invalid choice";

}

cout<<"\nDo you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}

Journal Program 7

 #include<iostream.h>

#include<conio.h>

#include<string.h>

#include<process.h>

#include<ctype.h>

class String

{

char str[100];

public:

void getData()

{

cout<<"Enter string : ";

cin>>str;

}

void strlen1()

{

int len;

len=strlen(str);

cout<<"\nLength of "<<str<< " is "<<len;

}

void strrev1()

{

cout<<"\nReverse of "<<str<<" is :"<<strrev(str);

}

void strupr1()

{

cout<<"\nUpper Case of string is ="<<strupr(str);

}

void strlwr1()

{

cout<<"\nLower Case of string is ="<<strlwr(str);

}


};

int main()

{

clrscr();

int choice;

char ch;

String s;

s.getData();

do

{

cout<<endl<<"1:Reverse String";

cout<<endl<<"2:Convert the string in Uppercase";

cout<<endl<<"3:Convert the string in Lowercase";

cout<<endl<<"4:String Length";

cout<<endl<<"5:Exit";


cout<<endl<<"\nEnter your choice=";

cin>>choice;

switch(choice)

{

case 1:

s.strrev1();

break;

case 2:

s.strupr1();

break;

case 3:

s.strlwr1();

break;

case 4:

s.strlen1();

break;

case 5: exit(0);

break;

default:

cout<<endl<<"Invalid choice";

}

cout<<"\nDo you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}

Journal PRogram 6

 #include<iostream.h>

#include<conio.h>

#include<string.h>

#include<process.h>

class String

{

char str1[100],str2[100];

public:

void getData()

{

cout<<"Enter first string : ";

cin>>str1;

cout<<"Enter second string : ";

cin>>str2;


}

void strlen1()

{

int len1,len2;

len1=strlen(str1);

len2=strlen(str2);

cout<<"\nLength of "<<str1<< " is "<<len1;

cout<<"\nLength of "<<str2<< " is "<<len2;


}

void strcpy1()

{

cout<<"\nAfter Copy String2 in to String1 :";

strcpy(str1,str2);

cout<<"\nString 1 ="<<str1;

}

void strcat1()

{

strcat(str1,str2);

cout<<"\nConcated String is ="<<str1;

}

void strcmp1()

{

if(strcmp(str1,str2)==0)

cout<<"\nTwo Strings are equal";

else

cout<<"\nTwo Strings are not equal";

}


};

int main()

{

clrscr();

int choice;

char ch;

String s;

s.getData();

do

{

cout<<endl<<"1:String Length";

cout<<endl<<"2:Copy in another string";

cout<<endl<<"3:To combine two string";

cout<<endl<<"4:To compare two strings";

cout<<endl<<"5:Exit";


cout<<endl<<"\nEnter your choice=";

cin>>choice;

switch(choice)

{

case 1:

s.strlen1();

break;

case 2:

s.strcpy1();

break;

case 3:

s.strcat1();

break;

case 4:

s.strcmp1();

break;

case 5: exit(0);

break;

default:

cout<<endl<<"Invalid choice";

}

cout<<"\nDo you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}