Cse 202 Assignment: Submitted By: Gaurav Pathak Reg - No: 11101366 Roll No.:RK1R06B29
Cse 202 Assignment: Submitted By: Gaurav Pathak Reg - No: 11101366 Roll No.:RK1R06B29
Cse 202 Assignment: Submitted By: Gaurav Pathak Reg - No: 11101366 Roll No.:RK1R06B29
Q1)
Write a program which has a class named SubString which has data members to hold two strings. The class also contains member functions which checks to see whether or not the second string is a substring of the first. Please do not make use of the substr or any other library function. Also the member function should be designed such that a * in the second string can match zero or more characters in the first string. Also a functionality should be included so as that when an asterisk is preceded by a \ then the asterisk should be taken to be a part of the string. Sample Run:Enter string1 abcd Enter string2 a*c Output:The second string is present in the first string.
Ans:
#include<iostream> #include<conio.h> using namespace std; class sub_string { char string[20]; char sub_string[20]; public: void get() { cout<<"Enter First String:\t"; cin>>string; cout<<"\nEnter Substring:\t"; cin>>sub_string; }
void search() { int i=0; int j,b_slash,l,m,s_len,len; j=0; b_slash=0; l=0; m=0; while(string[i]!='\0') { i++; } len=i; i=0; while(sub_string[i]!='\0') { i++; } s_len=i; i=0;
while(sub_string[j]!='\0') { if(sub_string[j]=='\\') { b_slash++; // counter for backslash } if(sub_string[j]=='\\' && sub_string[j+1]=='*') { m++; // counter for condition of '\' preceding '*' } j++; } j=0; if(m==0) // if string do not have '\' condition { while(string[i]!='\0') //loop till the end of the first string { if(sub_string[j]==string[i] || sub_string[j]=='*') // condition for '*' { l++; j++; } i++; } if(l==s_len) { cout<<"Yes It Is A SubString!!!"; //if counter and length of substring are equal } else { cout<<"No It Is Not A SubString"; } }
Screen shots: 1.
2.
3.
Q2)
WAP to read a string and rewrite it in the alphabetical order . For example: The word STRING should be written as GINRST.
Ans:
#include<iostream> #include<conio.h> #include<stdio.h> #include<string.h> using namespace std; int main() { int temp; char str[50]; cout<<"\nEnter The string:"; gets(str); for(int i=1;i<strlen(str);i++) { for(int j=0;j<strlen(str)-1;j++) { if(str[i]<str[j]) { temp=str[i]; str[i]=str[j]; str[j]=temp; } } } cout<<"\nString arranged in ascending order: "; for(int i=0;i<strlen(str);i++) { cout<<str[i]; } getch(); return 0; } //array of character data type
Screen Shots: 1.
2.
Q3)
Write a program which contains a class called BraceMatch. The class should contain data members capable of holding an expression. The expression will contain parenthesis (curly braces whose opening and closing braces should match). The class should contain a member function which is capable of reporting if the opening and closing braces match and in case they do not, it should report as to which is more (i.e are there more opening braces than closing braces or not) and how many are needed to complete a pair. Make use of a pointer to a class in order to achieve this. You may use constructors, any data member or member functions which in your opinion can help in solving the problem. Sample Run:Enter expression:- a+{v * c} +{{{{{y}}} Output:The opening braces are more than the closing braces. There are two more closing braces required to complete the pair.
Ans:
#include<iostream> #include<conio.h> #include<stdio.h> #include<string.h> using namespace std; class bracematch { char str[20]; public: void get() { cout<<"\nEnter the expression:"; gets(str); } void check(); }; void bracematch::check() { char ch1='}';
char ch='{'; char ch1='}'; int c=0,c1=0; for(int i=0;i<strlen(str);i++) { if(str[i]==ch) { c++; } else if(str[i]==ch1) { c1++; } }
//counters
//to check opening brace //if the condition satisfies counter is incremented //to check closing brace //if the condition satisfies counter is incremented
if(c==c1) // both counters are same { cout<<"\nSame number of opening and closing braces"; } else if(c<c1) //one counter is less than other { cout<<"\nThe Closing braces are more than Opening braces"; cout<<"\nThere are "<<c1-c<<" more opening braces required to complete the pair"; } else if(c>c1) //one counter is more than other { cout<<"\nThe Opening braces are more than Closing braces"; cout<<"\nThere are "<<c-c1<<" more closing braces required to complete the pair"; } }
//object created //pointer to a class bracematch //get() function is called //check() function is called
Screen Shots: 1.
2.
Q4)
Write a program which contains a class called Date which is capable of accepting the day, month and year. Utilize the concept of operator overloading to overload the + operator so that if the user enters either the day, month or year, the resultant date should be displayed. The binary operator should also be overloaded so that if the user enters either the day, month or year then the resultant date should be displayed. The = operator should be overloaded which should compare two dates and should report if those dates are equal or not. The class should also contain provision for displaying the date in the following formats:i) MM-DD-YY ii) DD-MM-YYYY iii) DD-Month Name-YY iv) DD-Month Name-YYYY Here DD stands for Day, MM stands for month and YY stands for last two digits of the year whereas YYYY contains 4 digits of the year. Month Name stands for the name of the month such as January, February etc.
Ans:
#include<iostream> #include<conio.h> #include<stdio.h> #include<process.h> #include<string.h> using namespace std; class dat { public: int date; int month; int year; char month1[10]; public: void getdate() { do { do { //member function to get a valid date //class declaration
//data members
cout<<"\nEnter Date(DD) :"; cin>>date; if(date>31) { cout<<"Invalid Entry"; } }while(date>31); do { cout<<"\nEnter Month(MM):"; cin>>month; if(month>12) { cout<<"invalid Entry"; } }while(month>12); cout<<"\nEnter Year(YYYY) :"; cin>>year; if(year%4!=0 && date>28 && month==2) //to check for leap year condition { cout<<"Invalid Date Entered\nEnter date again"; } }while(year%4!=0 && date>28 && month==2); } void operator+(); void operator-(); void operator=(dat); }; void dat::operator+() //member function definition { int i; int date1; int year1; int month2; clrscr(); char ch; do { clrscr(); cout<<"'+' Operator Overloading\n"; cout<<"\nSelect The Format Of Date\n\ //menu for selecting format of the date 1. MM-DD-YY\n\ 2. DD-MM-YYYY\n\ 3. DD-Month Name-YY\n\ 4. DD-Month Name-YYYY\n\ 5. Exit\n\ \nEnter Your Choice:"; cin>>i; //member function for + operator overloading //member function for - operator overloading //member function for = operator overloading
switch(i) { case 1:
//selecting any of the 4 formats int k; clrscr(); cout<<"\nMM-DD-YY"; //1st format cout<<"\nSelect:\n1. Month(MM)\n2. Date(DD)\n3. Year(YYYY)\n"; cin>>k; switch(k) //any of the 3 options can be selected to display the output { case 1: clrscr(); cout<<"\nEnter Month(MM):"; cin>>month2; if(month2==month) //check month are equal { cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else cout<<"\nInvalid Entry!!"; break; case 2: clrscr(); cout<<"\nEnter Date(DD):"; //check date are equal cin>>date1; if(date1==date) { cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else cout<<"\nInvalid Entry!!"; break; case 3: clrscr(); cout<<"\nEnter Year(YYYY)"; //check year are equal cin>>year1; if(year1==year) { cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice!!!"; } break;
//2nd format
cin>>p; switch(p) //select any of the 3 options to display output { case 1: clrscr(); cout<<"\nEnter Date(DD):"; cin>>date1; if(date1==date) //check date { cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break;
case 2:
clrscr(); cout<<"\nEnter Month(MM):"; //check month cin>>month2; if(month2==month) { cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break;
case 3:
clrscr(); cout<<"\nEnter Year(YYYY):"; //check year cin>>year1; if(year1==year) { cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice"; } break; case 3: //3rd format int q; clrscr(); cout<<"\nDD-Month Name-YY"; cout<<"Select:\n 1. Date(DD)\n 2. Month(MM)\n 3. Year(YYYY)\n"; cin>>q; switch(q) //select any of the 3 options to display output { case 1: clrscr(); cout<<"\nEnter Date(DD) :";
cin>>date1; if(date1==date) //check date { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; case 2: cout<<"\nEnter Month (MM):"; cin>>month2; if(month2==month) //check month { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY) :"; cin>>year1; if(year1==year) //check year { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; default: x: { switch(month) { case 1: case 2: strcpy(month1,"Feb"); break; case 3: strcpy(month1,"March"); break; case 4: strcpy(month1,"April"); break; case 5: strcpy(month1,"May"); break; case 6: strcpy(month1,"June"); break; cout<<"Wrong Choice!!"; // x statement definition //display corresponding month name strcpy(month1,"Jan"); break;
case 7:
strcpy(month1,"July"); break;
case 8: strcpy(month1,"August"); break; case 9: strcpy(month1,"September"); break; case 10: strcpy(month1,"October"); break; case 11: strcpy(month1,"November"); break; case 12: strcpy(month1,"December"); break; } cout<<"\nDate Is:"<<date<<"/"<<month1<<"/"<<year%100; } } break; case 4: //4th format int r; cout<<"\nDD-Month Name-YYYY"; cout<<"Select:\n1. Date(DD)\n2. Month(MM)\n3. Year(YYYY)\n"; cin>>r; switch(r) //select any of the 3 options to display the output { case 1: cout<<"\nEnter Date(DD) :"; cin>>date1; if(date1==date) //check date { goto y; //jump to y statement } else cout<<"\nInvalid Entry!!"; break; case 2: cout<<"\nEnter Month (MM):"; cin>>month2; if(month2==month) //check month { goto y; //jump to y statement } else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY) :"; cin>>year1; if(year1==year) //check year { goto y; //jump to y statement }
else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice!!"; y: { //y statement definition switch(month) { case 1: case 2: strcpy(month1,"Feb"); break; //display the corresponding month name strcpy(month1,"Jan"); break;
case 3:
strcpy(month1,"March"); break; case 4: strcpy(month1,"April"); break; case 5: strcpy(month1,"May"); break; case 6: strcpy(month1,"June"); break;
case 7:
strcpy(month1,"July"); break; case 8: strcpy(month1,"August"); break; case 9: strcpy(month1,"September"); break; case 10: strcpy(month1,"October"); break; case 11: strcpy(month1,"November"); break; case 12: strcpy(month1,"December"); break; } cout<<"\nDate Is:"<<date<<"/"<<month1<<"/"<<year; } } break; case 5: exit(0); break; default: cout<<"Wrong choice !!!";
}while(ch=='y'||ch=='Y'); }
void dat::operator-() { int i; int date1; int year1; int month2; char ch; do {
//do-while condition
cout<<"\n'-' Operator Overloading\n"; cout<<"\nSelect The Format Of Date\n\ 1. MM-DD-YY\n\ 2. DD-MM-YYYY\n\ 3. DD-Month Name-YY\n\ 4. DD-Month Name-YYYY\n\ 5. Exit\n\ \nEnter Your Choice:"; cin>>i; switch(i) { case 1: int k;
cout<<"\nMM-DD-YY"; //1st format cout<<"\nSelect:\n1. Month(MM)\n2. Date(DD)\n3.Year(YYYY)\n"; //menu cin>>k; switch(k) { case 1: cout<<"\nEnter Month(MM):"; cin>>month2; if(month2==month) //check month { cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else cout<<"\nInvalid Entry!!"; break; case 2: cout<<"\nEnter Date(DD):"; cin>>date1; if(date1==date) //check date {
cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY)"; cin>>year1; if(year1==year) //check year { cout<<"\nDate Is:"<<month<<"/"<<date<<"/"<<year%100; } else <<"\nInvalid Entry!!"; break;
default:
cout<<"Wrong Choice!!!"; } break; case 2: int p; cout<<"\nDD-MM-YYYY"; //2nd format cout<<"Select:\n 1.Date(DD)\n 2.Month(MM)\n 3.Year(YYYY)\n"; cin>>p; switch(p) { case 1: cout<<"\nEnter Date(DD):"; cin>>date1; if(date1==date) //check date { cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break; case 2: cout<<"\nEnter Month(MM):"; cin>>month2; if(month2==month) //check month { cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY):"; cin>>year1; if(year1==year) //check year { //default condition
//menu
cout<<"\nDate Is:"<<date<<"/"<<month<<"/"<<year; } else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice"; } break; case 3: //3rd format int q; cout<<"\nDD-Month Name-YY"; cout<<"Select:\n 1. Date(DD)\n 2. Month(MM)\n 3. Year(YYYY)\n"; cin>>q; switch(q) { case 1: cout<<"\nEnter Date(DD) :"; cin>>date1; if(date1==date) //check date { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; case 2: cout<<"\nEnter Month (MM):"; cin>>month2; if(month2==month) //check month { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY) :"; cin>>year1; if(year1==year) //check year { goto x; //jump to x statement } else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice!!"; x: { switch(month) { case 1: strcpy(month1,"Jan"); break;
//menu
strcpy(month1,"March"); break;
case 4: strcpy(month1,"April"); break; case 5: strcpy(month1,"May"); break; case 6: strcpy(month1,"June"); break; case 7:
strcpy(month1,"July"); break;
case 8: strcpy(month1,"August"); break; case 9: strcpy(month1,"September"); break; case 10: strcpy(month1,"October"); break; case 11: strcpy(month1,"November"); break; case 12: strcpy(month1,"December"); break; } cout<<"\nDate Is:"<<date<<"/"<<month1<<"/"<<year%100; } } break; case 4: //4th format int r; cout<<"\nDD-Month Name-YYYY"; cout<<"Select:\n1. Date(DD)\n2. Month(MM)\n3. Year(YYYY)\n"; cin>>r; switch(r) { case 1: cout<<"\nEnter Date(DD) :"; cin>>date1; if(date1==date) //check date { goto y; //jump to y statement } //menu
cout<<"\nEnter Month (MM):"; cin>>month2; if(month2==month) //check month { goto y; //jump to y statement
} else cout<<"\nInvalid Entry!!"; break; case 3: cout<<"\nEnter Year(YYYY) :"; cin>>year1; if(year1==year) //check year { goto y; //jump to y statement } else cout<<"\nInvalid Entry!!"; break; default: cout<<"Wrong Choice!!"; } break; case 5: exit(0); break; default: cout<<"Wrong choice !!!"; y: { switch(month) { case 1: strcpy(month1,"Jan"); break; //y statement definition //\month name corresponding to month number
case 2:
strcpy(month1,"Feb"); break; case 3: strcpy(month1,"March"); break; case 4: strcpy(month1,"April"); break; case 5: strcpy(month1,"May"); break;
case 6:
strcpy(month1,"June"); break;
case 7: strcpy(month1,"July"); break; case 8: strcpy(month1,"August"); break; case 9: strcpy(month1,"September"); break; case 10: strcpy(month1,"October"); break; case 11: strcpy(month1,"November"); break; case 12: strcpy(month1,"December"); break; } cout<<"\nDate Is:"<<date<<"/"<<month1<<"/"<<year; } } cout<<"\nBack To previous Menu(Y/N)?:"; ch=getche(); }while(ch=='y'||ch=='Y');
}
void dat::operator=(dat p) { int c=0; if(p.date==date) { c++; } if(month==p.month) { c++; } if(year==p.year) { c++; } //= operator overloading function definition //check date
//check month
//check year
if(c==3) // condition for dates to be same { cout<<"\n Dates are same "; } else cout<<"\n Different dates"; }
int main() { dat d1,d2; int i; char ch; cout<<"\nEnter A valid Date"; d1.getdate(); //getdate() function is called do { cout<<"\nSelect Option:\n 1. + overloading \n 2. - Overloading\n 3. '=' operator Overloading\n 4. Exit \nselect:"; cin>>i; switch(i) { case 1:
+d1; break;
case 2: -d1; break; case 3: //= operator overloading cout<<"\nEnter date to compare"; d2.getdate(); d2=d1; break; //- operator overloading
case 4: exit(0); break; default: cout<<"\nWrong Choice!!"; } cout<<"\nBack to Main Menu(y/n)?:"; ch=getche(); }while(ch=='y'||ch=='Y'); getch(); return 0; }
Screen Shots: 1. Firstly user have to enter a valid date ( Condition of Leap Year Should be kept in mind).
2. Main Menu
3. If the user select option 1. Then + operator overloading Menu showing different format appears.
4. Now if user select 4th format (option 4). Then following menu appers.
5. Now the user have to select any of the 3 options ( either date, month, year) . If the user selects option 2 ( Month), then month is entered by the user if the month matches with the corresponding month then Whole date is displayed.
6. If the user Enter value that does not match with the corresponding month then Invalid Entry comes as output.
7. If the user selects = operator overloading then , user have to enter a date, month and year to compare . If all the value matches then following output is displayed.
Q5)
A class is derived using private derivation. In private derivation ,all members of base class become private members of derived class. Is it possible , to make member function of base class as public member function of derived class? If yes, demonstrate with help of suitable example.
Ans:
member functions is highly limited. The private data members and member functions in the base class are inaccessible in the derived class. However, the derived class can use public members of the base class to access them if When the inheritance is private, the access to inherited data members and
necessary. The protected and public members and member functions become
private i.e we cannot use members or member function of the base class using derived class objects.
All members and member function become private members of the derived
When inheritance is private or protected the access level of members of the base may be more restrictive in the derived class than it was in the base. For eg:
Class base: //base class { public: void get() { Cout<<Hello; } Void show() { Cout<<Bye; } }; Class derived : private base { };
// private inheritance
In this hierarchy, get() is public in base but private in DERIVED. To make get() public in DERIVED we can add a using declaration for it to a public section in
DERIVED.
make the get() member accessible to users and show accessible to classes subsequently derived from DERIVED.
YES,
Class derived : private base { public: // maintain access levels for members related to the size of the object using base : : get; // earlier was private now is public . (can be used by the object of the derived class) using base : : show; // earlier was private now is public . (can be used by the object of the derived class) };
Just as we can use a using declaration to use names from std namespace , we may
also use a USING declaration to access a name from a base class. The form is the same except that the left-hand side of the scope operator is a class name instead of namespace name.
::
operator looks first the name of the right-hand operand in the scope of the
There are two ways to restore an inherited members access specification Access declaration method directly write base :
: get .
But in some compilers it gives warnings to use keyword using because Using statement are preferred over access declaration statements.
Warning
Example Showing how we can make to make member function of base class as public member function of derived class #include<iostream> #include<conio.h> using namespace std; class base { public: int a; int b; int cal; //base class // data members
void get_value() // member function { cout<<"\nEnter Value of A:"; cin>>a; cout<<"\nEnter Value of B:"; cin>>b; } void show() { cout<<cal; } };
class derived:private base { public: base::get_value; base::show; void add() { cal=a+b; } void mul() { cal=a*b; } void div() { cal=a/b; } void sub() { cal=a-b; } };
// private inheritance // earlier was private now is public // earlier was private now is public
int main() { derived d1; d1.get_value(); // get_value called using object of derived class cout<<"\nCalculation\n\n"; cout<<"\nAddition :"; d1.add(); d1.show(); cout<<"\nSubstraction :"; d1.sub(); d1.show(); cout<<"\nMultiplication:"; d1.mul(); d1.show(); cout<<"\ndivide :"; d1.div(); d1.show(); getch(); return 0; } Screen
Screen Shot:
Q6)
Write a program which has a class which contains a member function which is capable of generating the list of 100 numbers made up of all 1s and 0s which are divisible by 17. Assume that we are dealing with decimal number system. Include appropriate data members in the class. You may make use of any data members and member functions necessary to achieve the outcome.
Ans:
#include<iostream> #include<conio.h> using namespace std; class number_gen { public: long long number,multiple,len_check,number1; int c,c1,s,j;
number_gen() { multiple=1; j=0; c=0; c1=0; s=0; } void gen() { while(j!=100) { number=17*multiple;
multiple++;
} } }; int main() { number_gen n; cout<<"S.NO\t\t\tMULTIPLE\t\tNUMBER\n\n"; n.gen(); getch(); return 0; }
Screen Shots: