Download presentation
Presentation is loading. Please wait.
Published byWilliam McCormick Modified over 9 years ago
1
A program example is given below to input date and display on the screen by write a class ‘ date ‘ # include Class date { Private: Int y, m, d ; Publice : Void getdate ( void) } Cout <<” enter year “ ; cin <<y; Cout >m; Cout <<enter day “ ; cin<< d; } Viod printdate ( void) { cout <<” date is : “ << end1; Cout << d<< “/”<<m<<”/”<<y; } } ; Void main (void) { date D1; clrscr ( ); D1. Getdate ( ) ; D1. printdate ( ); } www.shahidrasul.com
2
Q. Write a program by using class ‘ Record ‘ to enter and display the data of a student on the computer screen. # include class Record { Private : char name [20]; int age; Public : Viod input (void) { cout <<“ Enter name of student “ ; cin>> name; cout <<”Enter age of student “ ; Cin >>age ; } Void print ( void) { cout << “ name of student : “ << name << endl; cout<<”age of student : ‘ << age << end1; } }; void main ( void) { Record obj; clrscr ( ) ; obj. input ( ) ; obj. print ( ) ; } www.shahidrasul.com
3
Write a program by using class to enter two value using member function of class. Display the sum of the two values by using another member function of the class. #include class sum { Private: int a, b; Public: Void input ( int x, int y) { a = x; b = y; } Void print (void) { cout << “ sum = “ << a+ b; } {; void main( void) { sum S1 ; Int a, b; clrscr( ) ; cout<<” Enter first value “ ; cin << a; cout<<”Enter second value ? ”; cin << b; S1. input ( a, b) ; S1.print ( ) ; } www.shahidrasul.com
4
Q. Write a class ‘ student ‘get the name of student and marks of three subjects. Calculate total marks and average marks. Each subject has maximum of 100 marks. Print the record of the student on the computer serene. #include class student { Private ; char name [17]; float S1,S2, S3, total, avg; Public: void input_ data ( void ) { cout << “ Enter name of student “ ; cin << name ; cout << “ Enter marks of 1st subject ”; cin << S1; cout<< “ Enter marks of 2 nd subject “ ; cin<<S2; cout << “Enter marks of 3 rd subject ? “ ; cin << S3; Total = S1 +S2 S3; Avg = toal /3.0; { void print_data ( void) { cout << endl; cout << “ name of student : “ << name <<endl; cout <<” marks of ist subject: “<<1<<endl; cout <<” marks of 2nd subject :”<<s2<<endl; cout <<” marks of 3 rd subject :”<<s3<<endl; cout<<” total marks: “ << total << endl; cout “ average marks: “<< avg << endl; } { continued….. continue…… www.shahidrasul.com
5
void main(void) Student object; clrscr ( ) ; Object. Input _ data ( ); Object. Print_ data ( ); } www.shahidrasul.com
6
Accessing data members using scope resolution operator The variable defined in a member function has scope and they are known only to that function. The variable name defined in a member function and the name data member may be of the same names. In this case, the data member hidden by the member function and cannot be accessed directly by the member function that contains the same variable name. #include class test { Private; int a, b; Public: void temp (void) { int b; B= 15; A =b+10; Test: :b= b +100; } void print ( void ) { cout <<”value of a : “ << a<< end 1; cout <<” value of b: “<< b<<end1; } }; void main (void ) Test obj; clrscr ( ) ; Obj. temp ( ); Obj. print ( ); } www.shahidrasul.com
7
Write a program using class circle to computer and display the area of a circle. The circle, should have the following members. #include class circle { Private :float r, area; Public: void compute_ area ( float n) { r = n; area = 3.1417*r*r; } void print _ area ( void) { cout << endl; cout<<”radius of circle =”<<r<<endl; cout<<”area of circle =” << area<<endl; } }; void main (void ) { Circle ac; Float radius ; clrscr (); cout <<”Enter radius of circle“; cin << radius; Ac. Comput_ area (radius); Ac.print_ area ( ); } www.shahidrasul.com
8
A program example is given below in which two. Class are defined. The member functions are defined outside the classes. #include class abc { Public: void temp (void ); }; class xyz { public; void temp ( void); }; void main(void) abc x; xyz y; clrscr ( ); x. temp( ); y. temp ( ); { void abc : : temp ( void) { Cout<<”MEMBER function of class abc “ << endl; } void xyz : : temp (void) { cout <<” member function of class xyz “ <<endl; } www.shahidrasul.com
9
write a program using class’ rectangle’ to computer the area & perimeter of rectangle. The class rectangle’ should have the following member:. #include class rectangle { private : float length, width; public: void setdata ( float, float ); void area (void); void perimeter( void); }; void main (void) { rectangle obj ; floatlen, w; clrscr ( ) cout << Enter length of rectangle ? ” ; cin << len; cout<< “ Enter width of rectangle ? “ ; cin << w; obj. setdata (len,w); obj.area ( ) ; obj. perimeter( ); } void rectangle: : setdata ( float x, float y) { if ( (x<=0&&x<=25.0) && (y<=0 && y<=25.0)) { Length = x; Width =y; { Else continued……. www.shahidrasul.com
10
{ cout << “ invalid length or width “ ; Exit (0); } { void rectangle : : area ( void) { cout << end1; cout << “ area of rectangle = “; cout << length*width <<end1; } void rectangle : : perimeter of rectangle = “ ; { cout << “ perimeter of rectangle = “ ; cout <<2* (length *width) << endl; } Output of the program Enter length of rectangle ? 8 Enter width of rectangle ? 17 Area of rectangle = 136 Perimeter of rectangle= 272 www.shahidrasul.com
11
Write a program using class ‘ time 1’ to print the give time in standard format. The class should have separate data members for hours, mints & seconds. The class also should have the following members function. #include class time 1 { Private: int hours, minutes, seconds; Public: void setdata (int, int, int,); void print _ time (void ); }; void main (void ) { Time 1 t t t ; Int h, m,s; clrscr ( ) ; cout<< “ enter hours “ ; cin <<h; cout << “ enter minutes“ ; cin << m; cout << “ enter seconds “ ; cin << s; T t t. print_n time ( ) ; } void time1 : : setdata (int h1, int,m1, int s1) { If ( h1>=0&& h1 =0&& m1<=59) && ( s1>=0& s1 <=59)) { Hours = h1; continued…. continued…… www.shahidrasul.com
12
Minutes = m1; Seconds =s1; } Else { cout << “ invalid time “; Exit (0) ; } { void time1 : : print _ time (void ) { cout << end1; cout << hours << “:”<< minutes<< “: “ seconds; } www.shahidrasul.com
13
Write a program using class ‘ date l’ to print the current date of your system using “ day/ month/ year” format. The class also should have the following member function:- #include class date 1 { Private : int day, month, year ; Public: void print _ date (void); }; Void main (void ) { Date1 ddd; clrscr ( ) ; Ddd. Setdata( ); } void dat1 : : setdata (void) { Date d; Getdate (&d); Day= d. da_ day; Month = d.da _ mon; Year = d. da _ year ; } Void date1 : : print _ date ( void) { cout << “ your system date : “ ; cout <<day << “/ “ << month<< “/ “ << year ; www.shahidrasul.com
14
Q.write a program using class ‘employee ‘to compute the allowances, net pay and print on the screen. The class also should have the following date member:., #include Class employee { Private : int emp _ no ; float basic_ pay, house_ rent, Medical_ allow, conveyance_ allow Net _ pay; Public: void input data (void); void compute _ pay (void ); void print _ pay (void ); }; void main(void ) { Employee emp ; Clrscr ( ); Emp. Inputdata( ) ; Emp. Compute _ pay( ); Emp. Print_ pay( ); } void employee: : inputdata (void ) { cout << “ enter employee,s number “ ; cin >>emp _ no ; cout << “ enter employee.s basic pay“ ; cin >> basic _ pay ; } continued…. www.shahidrasul.com
15
void employee: : compute _ pay ( void ) { house _ rent = basic _pay *45/100; Medical_ allow = basic _ pay*45/100; Conveyance_ allow = basic _ pay 10/100; Net_ pay = basic_ pay + house_ rent +medical _ allow+ conveyance _ allow; } void employee: : print_ pay (void ) { cout <<endl; cout << “employee number: “ << emp_ no<< end1; cout << “basic pay : “ << basic _ pay << end1; cout << “house rent : “ << house _ rent <<endl; cout << “medical allowance:” <<medical_ allowance <<end; cout <<” conveyance allowance : “ << Conveyance _ allow<< endl; cout << “calculated net pay : “ << net_ pay << endl; } www.shahidrasul.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.