Anda di halaman 1dari 15

LOVELY PROFESSIONAL UNIVERSITY

CSE ASSIGNMENT
SUBMITTED TO: MISS. SHEVETA SUBMITTED BY:

PART A Question 1: Define a structure employee whose members are code having maximum of 25 character, name having maximum 25 character, department having maximum of 20 character and join date a member of type date.WAP to print the names of those employees who have completed at least one year of service. Search the employees using function and pass the array of objects using call by address. Answer: PROGRAM: #include"iostream.h" #include"conio.h" #include"stdio.h" Struct employee { Char code[25],name[25],dept[20]; Struct date { Int d,m,y; }jd; }e[20];

Int main() { Int n,i,j; Void show(employee e[20],int n); Cout<<"enter the number of employee"; Cin>>n;

Cout<<"enter the details of employee\n code\nname\ndepartment\ndate of joining\n"; For(i=1;i<=n;i++) { Cout<<"for "<<i<<":\n"; Fflush(stdin); Gets(e[i].code); Gets(e[i].name); Fflush(stdin); Gets(e[i].dept); Cin>>e[i].jd.d>>e[i].jd.m>>e[i].jd.y; } Show(e,n); Getch(); Return 1; } Void show(employee e[],int n) { Int i,m,c; M=n;

For(i=1;i<=m;i++) { If((e[i].jd.y<2012)&&(e[i].jd.m<=10)) { Cout<<"name:";

Puts(e[i].name); C++; } } If(c==0) Cout<<"none have one year experience"; }

Output:

Question 2: 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.

ANSWER: PROGRAM: #include"iostream.h" #include"conio.h" #include"stdio.h" Struct student { Char name[20]; Int roll; Int marks; }s[20];

Int main() { Void roll_no(student s[20],int n); Void sort_names(student s[20],int n); Void high_marks(student s[20],int n); Int i,n,c; Cout<<"enter the number of students:"; Cin>>n; Cout<<"enter the name\nroll\n marks\n"; For(i=1;i<=n;i++) { Cout<<"\nenter for "<<i<<"student:"; Fflush(stdin);

Gets(s[i].name); Cin>>s[i].roll; Cin>>s[i].marks; } Cout<<"enter\n1.to search a roll number from the list\n 2.search a name who has maximum marks\n 3.sort list in ascending order of name"; Cout<<"\nenter your choice:"; Cin>>c; If(c==1) Roll_no(s,n); If(c==2) High_marks(s,n); If(c==3) Sort_names(s,n); If(c>3) Cout<<"\nenter valid choice"; Getch(); Return 1; } Void roll_no(student s[20],int n) { Int r,m,i; M=n; Cout<<"\nenter the roll number to search:"; Cin>>r; For(i=1;i<=m;i++)

{ If(s[i].roll==r) { Cout<<"\nname:"; Puts(s[i].name); Cout<<"\nmarks:"<<s[i].marks; } } Cout<<"\nno student with roll number"<<r<<"exist"; } Void sort_names(student s[20],int n) { Struct student tmp; Int i,j,m; M=n; For(i=1;i<=m;i++) { For(j=1;j<=m;j++) If(strcmpi(s[i].name,s[j].name)<=0) { Tmp=s[i]; S[i]=s[j]; S[j]=tmp; } }

For(j=1;j<=n;j++) { Puts(s[j].name); } } Void high_marks(student s[20],int n) { Int i,p,max,m; M=n; Max=s[0].marks; For(i=1;i<=n;i++) { If(s[i].marks>max) { Max=s[i].marks; P=i; } } Cout<<"\nstudent securing highest marks:"; Cout<<"\nname:"; Puts(s[p].name); Cout<<"\nroll number:"<<s[p].roll; }

Output:

Question 3: Create a class account that stores the account number and balance amount of the depositor. Also create the member functions to assign initial values to deposited an amount, to withdraw an amount after checking the balance and finally display the balance.

ANSWER: PROGRAM: #include"iostream.h" #include"conio.h" Class account { Long int acc_no,bal; Public: Void getdata() { Cout<<"\nenter the acc_no and balance amount\n:"; Cin>>acc_no>>bal; }

Int check(long int b) { Int c,amnt; C=b; Amnt=bal-c;

If(bal>c) Cout<<"\n balance remained after withdrawal is:"<<amnt; Else Cout<<"\n unsufficent balance please enter the withdrawal amount below:"<<bal; } }; Int main() { Long int a; Account acc; Cout<<"\n enter the details of account:"; Acc.getdata(); Cout<<"\n enter the amont to withdraw:"; Cin>>a; Acc.check(a); Getch(); Return 1; }

Output:

PART B Question 4: If both base class and derive class contains same named public member function f1(),then which version of f1( ) will be invoked when called through an object of the derived class?

ANSWER: The mechanism of deriving a new class from an old one is called inheritance(or derivation). The old class is referred to as the source class and the new one derived from the old class(source class) is known as derived class or subclass. The derived class inherits some or all of the traits from the base class. If both base class and derived class contains same named public member function f1(),then the derived class function f1() supersedes the base class definition i.e. The function

f1() defined in derived class will be invoked when called through an object of the derived class.

Question 5: If C1 and C2 of the two objects of class complex then will the statement C2=5.0+C1; Work by overloading operator + which is a member function of class complex?

ANSWER : The general form of representation of addition of two complex object using operator overloading is C3=c1+c2; // invokes operator+() function As we know a member function can be invoked only by an object of the same class. Here, the object c1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function. So it is necessary that the operand on the left must be an object of the same class while the operand on the left may be the object of same class or any integer value. If c1 and c2 are the two objects of class complex then the statement C2=5+c1 ; Will not work by overloading operator + which is a member function of class complex because the left hand operand which is responsible for invoking the member function must be an object of the same class wehre as in the given expression it is an integer value. So this expression will not work by overloading + operator.

Question 6: If object X and object Y are two objects of different classes X and Y respectively then how the statement Objx=objy; Works?

Answer: The statement objX=objY ; is one of the type conversion statement used for conversion from one class type to another class type. Obj X is the object of Class X and obj Y is the object of class y. The classy type data is converted to the class X type data and the converted data value is assigned to the oject of class X i.e.objX, since the conversion takes place from classy to classx, Y is known as the source class and X is known as the destination class. Such conversion can be carried out either by a constructor or a conversion function.

Anda mungkin juga menyukai