oops concept all in prog

Upload: ashishpatrick1985

Post on 01-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Oops Concept All in Prog

    1/18

    1Define a class student with the following specificationPrivate membersof class studentadmno integersname 20 charactereng. math, science floattotal floatctotal() a function to calculate eng + math +science with float return type.Public memberfunction of class studentTakedata() Function to accept values for admno,sname, eng, science and invoke ctotal() tocalculate total.Showdata() Function to display all the data members

    on the screen.

    C++ Program to Deine a Class Student and accessingmember unction using its ob!ect#includeiostream.h!#includeconio.h!#includestdio.h!

    classstudent"private

    intadmno$charsname%20&$floateng,math,science$floattotal$floatctotal'("

    returneng+math+science$)

    publicvoid*akedata'("

    coutnter admission number $

  • 8/9/2019 Oops Concept All in Prog

    2/18

    cin!!admno$coutnter student name $gets'sname($coutnter marks in english, math, science $cin!!eng!!math!!science$total-ctotal'($

    )

    voidhowdata'("

    cout/dmission number admnontudentname snamennglish

    engn1ath mathnciencesciencen*otal total$

    )

    )$

    intmain'("

    clrscr'($student ob$ob.*akedata'($ob.howdata'($getch'($return0$

    )

    "Define a class batsman with the following specificationsPrivate members#bcode 3 digits code numberbname 20 characters

    innings, notout, runs integer typebatavg it is calculated according to the formula4

    batavg -runs5'innings6notout(calcavg() Function to compute batavgPublic members#readdata() Function toaccept value from bcode,name, innings, notout and invoke the function calcavg()dis$la%data() Function to display the data members

  • 8/9/2019 Oops Concept All in Prog

    3/18

    on the screen.

    C++ Program to Deine a Class &atsman and accessingmember unction using its ob!ect#includeiostream.h!#includeconio.h!

    #includestdio.h!

    classbatsman"

    intbcode$charbname%20&$intinnings,notout,runs$intbatavg$

    voidcalcavg'("batavg-runs5'innings6notout($

    )

    publicvoidreaddata'($voiddisplaydata'($

    )$

    voidbatsmanreaddata'("

    coutnter batsman code $cin!!bcode$coutnter batsman name $gets'bname($coutenter innings,notout and runs $

    cin!!innings!!notout!!runs$calcavg'($

    )

    voidbatsmandisplaydata'("

    cout7atsman code bcoden7atsman namebnamen8nnings innings

  • 8/9/2019 Oops Concept All in Prog

    4/18

    n9ot out notoutn:uns runsn7atting/verage batavg$)

    intmain'("

    batsman ob$ob.readdata'($ob.displaydata'($getch'($return0$

    )

    'Define a class ** in ;++ with following descriptionPrivate embers

    *est;ode of type integerDescription of type string9o;andidate of type integer;enter:e

  • 8/9/2019 Oops Concept All in Prog

    5/18

    charDescription%C0&$int9o;andidate$int;enter:e

  • 8/9/2019 Oops Concept All in Prog

    6/18

    &* integer type&*TT- 20 charactersP/C float 'price per copy(TT0-CST() / function to calculate the total cost for 9number of copies where 9 is passed to the function asargument.Public membersof the class 7E are

    P2T() function to read 7E9. 7E*8*=,B:8;P2/C30S() function to ask the user to input the numberof copies to be purchased. 8t invokes **/=;*'( and printsthe total cost to be paid by the user.9ote Gou are also re

  • 8/9/2019 Oops Concept All in Prog

    7/18

    cin!!B:8;$)

    voidB@:;?/'("

    intn$coutnter number of copies to purchase $

    cin!!n$cout*otal cost is $**/=;*'n($

    ))$

    intmain'("

    7E ob$

    ob.89B@*'($ob.B@:;?/'($getch'($return0$

    )

    I. Define a class :B:* with the following specificationPrivate members #adno 3 digit admission numbername 20 charactersmarks an array of I floating point valuesaverage average marks obtainedJ*/KJ'( a function to compute the average obtainedin five subectPublic members#:/D89F'( function to accept values for adno, name,marks. 8nvoke the function J*/KJ'(

    D8B=/G89F'( function to display all data members ofreport on the screen.Gou should give function definitions.

    C++ Program to Deine a Class /P/T and accessingmember unction using its ob!ect

    #includeiostream.h!

  • 8/9/2019 Oops Concept All in Prog

    8/18

    #includestdio.h!#includeconio.h!

    class:B:*"

    intadno$charname%20&$

    floatmarks%I&$floataverage$voidJ*/KJ'("

    average-'marks%0&+marks%>&+marks%2&+marks%C&+marks%3&(5I$

    )

    publicvoid:/D89F'($voidD8B=/G89F'($

    )$

    void:B:*:/D89F'("

    do"

    coutnter 3 digit admission number $cin!!adno$

    )while'adnoLLLMMadno!>0000($

    coutnter name$gets'name($

    coutnter marks in $

    for'inti-0$iI$i++("

    coutubect i+>$

    cin!!marks%i&$)$

  • 8/9/2019 Oops Concept All in Prog

    9/18

    J*/KJ'($)

    void:B:*D8B=/G89F'("

    cout/dmission numberadno 9amename1arks aremarks%0& marks%>&

    marks%2& marks%C& marks%3&/verageaverage$)

    intmain'("

    :B:* ob$ob.:/D89F'($ob.D8B=/G89F'($

    getch'($return0$

    4 Nrite the definition for a class called /ectanglethat hasfloating point data members length and width. *he class has

    the following member functionsvoid setlength(loat)to set the length data membervoid setwidth(loat)to set the width data memberloat $erimeter()to calculate and return the perimeter of therectangleloat area()to calculate and return the area of the rectanglevoid show()to display the length and width of the rectangleint same0rea(/ectangle)that has one parameter of type

    :ectangle. same/rea returns > if the two :ectangles have thesame area, and returns 0 if they donOt.

    >. Nrite the definitions for each of the above member functions.2. Nrite main function to create two rectangle obects. et thelength and width of the first rectangle to I and 2.I. et thelength and width of the second rectangle to I and >P.L.Display each rectangle and its area and perimeter.C. ;heck whether the two :ectangles have the same area and

  • 8/9/2019 Oops Concept All in Prog

    10/18

    print a message indicating the result. et the length and widthof the first rectangle to >I and Q.C. Display each :ectangleand its area and perimeter again. /gain, check whether the two :ectangles have the same area and print a messageindicating the result..

    0nswer#

    Nrite the definition for a class called :ectangle that hasfloating point data members length and width. *he class hasthe following member functionsvoid setlength'float( to set the length data membervoid setwidth'float( to set the width data memberfloat perimeter'( to calculate and return the perimeter of the

    rectanglefloat area'( to calculate and return the area of the rectanglevoid show'( to display the length and width of the rectangleint same/rea':ectangle( that has one parameter of type:ectangle. same/rea returns > if the two :ectangles have thesame area, and returns 0 if they donOt.#includeiostream.h!

    #includeconio.h!

    class:ectangle"

    privatefloatlength$floatwidth$

    publicvoidsetlength'float($voidsetwidth'float($floatperimeter'($floatarea'($voidshow'($intsame/rea':ectangle($

    )$

    http://cppforschool.com/assignment/class-object-sol/class-rectangle.htmlhttp://cppforschool.com/assignment/class-object-sol/class-rectangle.html
  • 8/9/2019 Oops Concept All in Prog

    11/18

    void:ectanglesetlength'floatlen("

    length-len$)

    void:ectanglesetwidth'floatwid(

    "width-wid$

    )

    float:ectangleperimeter'("

    return'2Hlength+2Hwidth($)

    float:ectanglearea'("

    returnlengthHwidth$)

    void:ectangleshow'("

    cout=ength length Nidth width$)

    int:ectanglesame/rea':ectangle other("

    floatareaf-lengthHwidth$floatareas-other.lengthHother.width$if'areaf--areas(

    return>$

    return0$)

    intmain'("

    :ectangle first$:ectangle second$first.setlength'I($

  • 8/9/2019 Oops Concept All in Prog

    12/18

    first.setwidth'2.I($second.setlength'I($second.setwidth'>P.L($coutFirst rectangle $first.show'($coutendl/rea first.area'( Berimeter

    first.perimeter'( endlendl$

    coutecond rectangle $second.show'($coutendl/rea second.area'( Berimeter

    second.perimeter'( endlendl$if'first.same/rea'second((

    cout:ectangles have the same arean$else

    cout:ectangles do not have the same

    arean$first.setlength'>I($first.setwidth'Q.C($coutFirst rectangle $first.show'($coutendl/rea first.area'( Berimeter

    first.perimeter'( endlendl$coutecond rectangle $second.show'($coutendl/rea second.area'( Berimeter

    second.perimeter'( endlendl$if'first.same/rea'second((

    cout:ectangles have the same arean$else

    cout:ectangles do not have the samearean$

    getch'($

    return0$)

    5 Nrite the definition for a class called com$le6that has floatingpoint data members for storing real and imaginary parts. *heclass has the following member functionsvoid set(loat, loat)to set the specified value in obect

  • 8/9/2019 Oops Concept All in Prog

    13/18

    void dis$()to display compleR number obectcom$le6 sum(com$le6)to sum two compleR numbers Areturn compleR number>. Nrite the definitions for each of the above member functions.2. Nrite main function to create three compleR number obects.et the value in two obects and call sum'( to calculate sumand assign it in third obect. Display all compleR numbers.

    0nswer#%* 42&Nrite the definition for a class called com$le6that has floatingpoint data members for storing real and imaginary parts. *heclass has the following member functionsvoid set(loat, loat)to set the specified value in obectvoid dis$()to display compleR number obect

    com$le6 sum(com$le6)to sum two compleR numbers Areturn compleR number#includeiostream.h!#includeconio.h!

    classcompleR" private

    floatR$floaty$

    publicvoidset'floatreal,floatimg("

    R-real$y-img$)

    compleR sum'compleR($voiddisp'($

    )$

    compleR compleRsum'compleR ;("

    compleR t$t.R-R+;.R$

  • 8/9/2019 Oops Concept All in Prog

    14/18

    t.y-y+;.y$returnt$

    )

    voidcompleRdisp'("

    coutR + yendl$

    )

    intmain'("

    compleR ;>,;2,;C$

    ;>.set'2.I,S.>($

    ;2.set'3.2,I.I($;C-;>.sum';2($

    coutn compleR 9umber > - $;>.disp'($coutn compleR 9umber 2 - $;2.disp'($coutn compleR 9umber C - $;C.disp'($

    getch'($return0$

    )

    7 Nrite the definition for a class called Distancethat has datamember feet as integer and inches as float. *he class has thefollowing member functions

    void set(int, loat)to give value to obectvoid dis$()to display distance in feet and inchesDistance add(Distance)to sum two distances A returndistance>. Nrite the definitions for each of the above member functions.2. Nrite main function to create three Distance obects. et thevalue in two obects and call add'( to calculate sum and assignit in third obect. Display all distances..

    http://cppforschool.com/assignment/class-object-sol/class-distance.htmlhttp://cppforschool.com/assignment/class-object-sol/class-distance.html
  • 8/9/2019 Oops Concept All in Prog

    15/18

    0nswer#Nrite the definition for a class called Distancethat has datamember feet as integer and inches as float. *he class has thefollowing member functionsvoid set(int, loat)to give value to obect

    void dis$()to display distance in feet and inchesDistance add(Distance)to sum two distances A returndistance#includeiostream.h!#includeconio.h!

    classDistance" private

    intfeet$floatinches$

    publicvoidsetdist'intft,floatin("

    feet-ft$inches-in$)Distance add'Distance($voiddisp'($

    )$

    Distance Distanceadd'Distance D("

    Distance t$

    t.inches-inches+D.inches$t.feet-0$if't.inches!->2.0("

    t.inches6->2.0$t.feet++$

    )t.feet+-feet+D.feet$

  • 8/9/2019 Oops Concept All in Prog

    16/18

    returnt$)

    voidDistancedisp'("

    coutfeetOinches $)

    intmain'("

    Distance d>,d2,dC$d>.setdist'>0,S.>($d2.setdist'2C,I.I($dC-d>.add'd2($

    coutn distance > - $d>.disp'($coutn distance 2 - $d2.disp'($coutn distance C - $dC.disp'($

    getch'($return0$

    )

    8 Nrite the definition for a class called timethat has hours andminutes as integer. *he class has the following memberfunctionsvoid settime(int, int)to set the specified value in obectvoid showtime()to display time obecttime sum(time)to sum two time obect A return time

    >. Nrite the definitions for each of the above member functions.2. Nrite main function to create three time obects. et thevalue in two obects and call sum'( to calculate sum and assignit in third obect. Display all time obects..

    /nswer#includeiostream.h!#includeconio.h!

    http://cppforschool.com/assignment/class-object-sol/class-time.htmlhttp://cppforschool.com/assignment/class-object-sol/class-time.html
  • 8/9/2019 Oops Concept All in Prog

    17/18

    classtime" private

    inthours$intminutes$

    publicvoidsettime'inth,intm("

    hours-h$minutes-m$)time sum'time($voidshowtime'($

    )$

    time timesum'time *1("

    time t$t.minutes-minutes+*1.minutes$t.hours-t.minutes5Q0$t.minutes-t.minutesTQ0$t.hours+-hours+*1.hours$returnt$

    )

    voidtimeshowtime'("

    couthours hours and minutesminutesendl$)

    intmain'("

    time *>,*2,*C$*>.settime'2,3I($*2.settime'C,C0($*C-*>.sum'*2($

  • 8/9/2019 Oops Concept All in Prog

    18/18

    coutn *ime > $*>.showtime'($coutn *ime 2 $*2.showtime'($coutn *ime C $*C.showtime'($

    getch'($return0$

    )