You are on page 1of 19

Q1.

Write a program to create a structure student and declare data members such as: stu_id, stu_name, stu_address,stu_section, contact_no,total_marks and member functions to a. Input data for N students b. Display data for N students. c. Search students who has more than 80 marks. d. Sort students according to name. Use functions, nested structures where required.

1.solu:
#include<iostream.h> #include<conio.h> struct student{ int stu_id,contact_no,total_marks; char stu_name[10],stu_address[10],stu_section[5]; }s[20]; void display(struct student); main(){ cout<<"enter the information of students like:stu_id, stu_name, stu_address,stu_section, contact_no,total_marks"; int l,i,j; cout<<"enter the number of students to have data:"; cin>>l; for(i=0;i<l;i++){ cin>>s[i].stu_id; gets(s[i].stu_name); gets(s[i].stu_address); gets(s[i].stu_section);

cin>>s[i].contact_no; cin>>s[i].total_marks; cin.ignore(); } for(j=0;j<l;j++){ display(s[j]); } for(i=0;i<l;i++){ if(s[i].total_marks>80){ display(s[i]); } } char dataval[5][10];

for(i=0;i<l;i++) { for(int j=0;j<7;j++) {

dataval[i][j]=s[i].stu_name[j];

}}

cout<<endl;

cout<<"here are the sorted list of names"<<endl;

for(k=97;k<123;k++) { for(i=0;i<l;i++) { if(dataval[i][0]==k) {for(int j=0;j<7;j++) { cout<<dataval[i][j];

} cout<<endl; }

} }

getch(); } void display(struct student s1){

cout<<s1.stu_id<<s1.stu_name<<s1.stu_address<<s1.stu_section<<s1.contact_no<<s1.total_marks; }

Q2. Create a class to represent a bank account. It should include Data Members: Name of account holder Account no. Type of Account Bank Account Member Function: To initialize the data members with appropriate data To deposit amount To withdraw amount after checking the balance To display details of account of account holder.

2.solu:
#include"iostream.h" #include"conio.h" class bankacc{ char nameofaccholder[15],typeofacc[15]; int accno,balance,b; public: void getdata(); void depositamt(); void withdrawamt(); void putdata(); }; void bankacc::getdata(){ cout<<"enter the information name,accno,type of account:"; gets(nameofaccholder); cin>>accno; gets(typeofacc); cin.ignore(); }

void bankacc::depositamt(){ cin.ignore(); cout<<"enter the balance:"; cin>>balance; } void bankacc::withdrawamt(){ cout<<"enter the amt to withdraw:"; cin>>b; if(b<balance){ cout<<"amount withdrawn"<<b; } else{cout<<"unable to proceed";} } void bankacc::putdata(){ int c; cout<<"details of acc holder:"; puts(nameofaccholder); cout<<accno; puts(typeofacc); if(balance>b){ c=balance-b; cout<<"available balance after withrawn:"<<c; } else{ cout<<"available balance after withrawn:"<<balance;

} } main(){ bankacc ba; ba.getdata(); ba.depositamt(); ba.withdrawamt(); ba.putdata(); getch(); }

Q3: WAP to perform following operations: a. Search a roll number from the list. b. Search a name who has maximum marks in the list. c. Sort the list according to name. Use arrays and structures where required and illustrate the concept of function overloading.

3.solu:
#include"iostream.h" #include"conio.h" struct student{ int roll_no; char name[10]; int marks; }s[10]; int main(){ int n,i,j,r,c=0; int temp; cout<<"enter the number of students not more than 10:";

cin>>n; cout<<"enter the data of students i.e. roll,name,marks:"; for(i=0;i<n;i++){ cin>>s[i].roll_no; cin.ignore(); gets(s[i].name); cin.ignore(); cin>>s[i].marks; } cout<<"entered data of students are as follows:"; for(i=0;i<n;i++){ cout<<s[i].roll_no; cin.ignore(); puts(s[i].name); cin.ignore(); cout<<s[i].marks; } cout<<"enter the roll number to search:"; cin>>r; for(i=0;i<n;i++){ if(s[i].roll_no==r){ cout<<"found"; } else{cout<<"not found";} }

char dataval[5][10];

for(i=0;i<l;i++) { for(int j=0;j<7;j++) {

dataval[i][j]=s[i].name[j];

}}

cout<<endl; cout<<"here are the sorted list of names"<<endl;

for(k=97;k<123;k++) { for(i=0;i<l;i++) { if(dataval[i][0]==k) {for(int j=0;j<7;j++) { cout<<dataval[i][j];

} cout<<endl; }

} } getch(); }

Concept of function overloading: In function overloading we use the same function name for various times for different intentions. It is also known as function polymorphism. The function which is overloaded must be different in their argument list. Example: #include<iostream.h> #include<conio.h> int sqr(int); float sqr(float); main(){ int a=5; float b=2.5; cout<<square:<<sqr(a); cout<<square:<<sqr(b); getch(); } int sqr(int s){

return(s*s); } float sqr(float j){ return(j*j); } o/p: square:25 square:6.25

Q4. A. Differentiate between static data members and static member function with example. B. How concept of reusability be implemented in C++? How much memory will be allocated to derived classes.

4. A. static data members and static member functions:


When a data member is declared as static it is initialized to zero. It is local to the data member. A static data item is helpful when all the objects of the class should share a common data. When a function is declared as static,it can access only static member variables and functions of the same class. The static keyword makes the function free from individual object of the class and its scope is global in the class. It doesnt create any sideeffects on other parts of the program. It is called in the program by using its class name without using its objects. As for example program containing both static data members and static member functions: #include<iostream.h> #include<conio.h> class sample{ static int c,k; public: static void count(){ c++; } static void show(){

cout<<number:<<++k; cout<<value of c:<<c; } }; int sample::c=0; int sample::k=0; main(){ sample::show(); sample a1,a2; sample::count; a1.count(); a2.count(); sample::show(); getch(); } Output of the above program: Number:1 Value of c:0 Number:2 Value of c:3

B.solu:
The idea of reusability came from the concept of inheritance. It means that we can add additional features or data to an existing class without modifying it or without changing the basic class. This is possible by deriving a new class from the existing one. The new class will have the combined features of both the classes. The power of the inheritance is that it allows the programmer to reuse a class called basic class by creating a new class called derived class which doesnt gave any impact on rest of the classes. Example for reusability: #include<iostream.h>

#include<conio.h>

class abc{ public: int a; int b; }; class def:public abc{ int c; int d; public: void get(){ cin>>a>>b>>c>>d; } void display(){ cout<<a<<b<<c<<d; } }; main(){ def ob; ob.get(); ob.display(); getch(); } Total memory allocated to derived class will be equal to the sum of memory taken by data members and member functions of base class and the memory taken by the data members and member function of derived class.

Q5. How Inline function and friend function are used in the classes illustrate with an example and state the advantages of using these functions.

5.solu:
The inline is used as a function specifier. The inline is a hint to the compiler that inline substitution of the function body is to be preferred to the usual function call implementation. When function execution completes, control goes back to the calling function and executes the next line of the program. Advantages of using inline function: It increases the execution speed, They are compact function calls, The size of the object code is reduced. Program to illustrate the use of inline function: #include<iostream.h> #include<conio.h> class sample{ int a; public: void getdata(); void putdata(); }; inline void sample::getdata(){ cout<<enter the number; cin>>a; } inline void sample::display data(){ cout<<the entered number is:<<a; } int main(){

sample ob; ob.getdata(); ob.displaydata(); } Output of above program: Enter the number 3 The entered number is:3

Friend function: Generally the private members of the class are accessed only by the member function.any non member function can nat access the private data. but in c++ non member function has an access to the private members of the class. We declare a non member function friend to the class whose privwte data to be accessed does this. The friend is a keyword. Advantages of using friend function: We can access private data by non member function through friend, Friend function also have inline member functions. Program to illustrate the use of friend function: #include<iostream.h> #include<conio.h> class sample{ int a; public: void getdata(); friend void display data(class sample); }; void sample::getdata(){

cout<<enter the number; cin>>a; } void display(class sample ab){ cout<<the entered number is:<<ab.a; } int main(){ sample ob; ob.getdata(); display(ob); } Output of the above program: Enter the number 20 The entered number is :20

Q6. Create a class called CheckSubstring. Inside this class, declare a public and private functions: - Ask the user to enter a line of text - Read a line of text from the keyboard - Ask the user to enter a second line of text - Read a second line of text from the keyboard Display whether one of the lines of text read from the keyboard contains the other as a substring. If one of the lines of text indeed contains the other line as a substring, then the function should also display the smallest index in the containing line at which the other line occurs. Use the methods defined in the String class to determine whether a String is a substring of another String, and if so, the first position in the superstring at which the substring appears. Sample session:

Enter a line of text: def Enter another line of text: abcdef "def" occurs in "abcdef" at position 3 "abcdef" is not a substring of "def" use the concept of nesting functions.

6.solu:
#include"iostream.h" #include"conio.h" class checksubstring{ char string1[10],string2[10]; public: void gettext1(); void gettext2(); void puttext1(); void puttext2(); void displaysubstring(); }; void checksubstring::gettext1(){ gets(string1); } void checksubstring::gettext2(){ gets(string2); } void checksubstring::puttext1(){ puts(string1);

} void checksubstring::puttext2(){ puts(string2); } void checksubstring::displaysubstring(){

int l,l1,t; l=strlen(string1); l1=strlen(string2); char temp[15];

int m=0,k=0; for(int j=m;j<l;j++) { if(string1[j]==string2[k]) { m=j+1; k++;

} }

for(t=0;t<l1 ;t++) {

if(string1[t]==string2[0]) { break;

} } if(k==l1 )

{ cout<<"substring and at location:"<<t; }else { cout<<"not substring"<<endl; }

main(){ checksubstring ob1; ob1.gettext1(); ob1.gettext2(); ob1.puttext1(); ob1.puttext2(); ob1.displaysubstring(); getch();

You might also like