You are on page 1of 14

LAB ASSIGNMENT C++

1. WAP to use Scope Resolution Operator.


#include <iostream> using namespace std; class X { public: static int count; }; int X::count = 10; int main () { int X = 0; cout << X::count << endl; }

// define static data member

// hides class type X // use static member of class X

2. WAP to Use COPY Constructor.


#include<iostream.h> #include<conio.h> class copy { int var,fact; public: copy(int temp) { var = temp; } double calculate() { fact=1; for(int i=1;i<=var;i++) { fact = fact * i; } return fact; } }; void main() { clrscr(); int n; cout<<"\n\tEnter the Number : "; cin>>n; copy obj(n); copy cpy=obj; cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate(); cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate(); getch(); }

LAB ASSIGNMENT C++


3. WAP to Use Destructor. 4. WAP to use Friend Function.
#include<iostream.h> #include<conio.h> class base { int val1,val2; public: void get() { cout<<"Enter two values:"; cin>>val1>>val2; } friend float mean(base ob); }; float mean(base ob) { return float(ob.val1+ob.val2)/2; } void main() { clrscr(); base obj; obj.get(); cout<<"\n Mean value is : "<<mean(obj); getch(); }

Output:
Enter two values: 10, 20 Mean Value is: 15

5. WAP to use Static data members.


#include<iostream.h> #include<conio.h> class stat { int code; static int count; public: stat() {

LAB ASSIGNMENT C++


code=++count; } void showcode() { cout<<"\n\tObject number is :"<<code; } static void showcount() { cout<<"\n\tCount Objects :"<<count; } }; int stat::count; void main() { clrscr(); stat obj1,obj2; obj1.showcount(); obj1.showcode(); obj2.showcount(); obj2.showcode(); getch(); }

Output:
Count Objects: 2 Object Number is: 1 Count Objects: 2 Object Number is: 2

6. WAP to implement STACK using DATASTRUCTURE. 7. WAP to overload Unary Operator (-).
#include<iostream.h> #include<conio.h> class complex { int a,b,c; public: complex(){} void getvalue() { cout<<"Enter the Two Numbers:"; cin>>a>>b; }

LAB ASSIGNMENT C++


void operator++() { a=++a; b=++b; } void operator--() { a=--a; b=--b; } void display() { cout<<a<<"+\t"<<b<<"i"<<endl; } }; void main() { clrscr(); complex obj; obj.getvalue(); obj++; cout<<"Increment Complex Number\n"; obj.display(); obj--; cout<<"Decrement Complex Number\n"; obj.display(); getch(); }

Output:
Enter the two numbers: 3 6 Increment Complex Number 4+ 7i Decrement Complex Number 3+ 6i

8. WAP to overload Binary Operator (+).


#include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:"; cin>>a>>b;

LAB ASSIGNMENT C++


} complex operator+(complex ob) { complex t; t.a=a+ob.a; t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { cout<<a<<"+"<<b<<"i"<<"\n"; } }; void main() { clrscr(); complex obj1,obj2,result,result1; obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:\n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); }

Output:
Enter the value of Complex Numbers a, b 4 5 Enter the value of Complex Numbers a, b 2 2 Input Values 4 + 5i 2 + 2i Result 6 + 7i 2 + 3i

LAB ASSIGNMENT C++


9. WAP to use Operator Overloading. 10. WAP to use Inline Function.
#include<iostream.h> #include<conio.h> class line { public: inline float mul(float x,float y) { return(x*y); } inline float cube(float x) { return(x*x*x); } }; void main() { line obj; float val1,val2; clrscr(); cout<<"Enter two values:"; cin>>val1>>val2; cout<<"\nMultiplication value is:"<<obj.mul(val1,val2); cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2); getch(); }

Output:
Enter two values: 5 7 Multiplication Value is: 35 Cube Value is: 25 and 343

11. WAP for Overload and Operator for adding two String.
Concatenate two strings using operator overloading in c plus plus?

Computer Programming Questions

LAB ASSIGNMENT C++


Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > C++ Programming Best Answer #include "stdafx.h" #include<iostream> #include<conio.h> using namespace std; class Add{ public: char *rep; Add(){} Add(char *tem){ rep = new char[strlen(tem)+1] ; strcpy(rep,tem); } Add operator + (const Add &rhs) { char *temp; temp= new char[strlen(rep) + strlen(rhs.rep)+1]; temp = strcat(rep,rhs.rep); return Add(temp); } }; int main() { cout<<"hello"; Add obj1("pradeep"); Add obj2("bansal"); Add obj3; obj3 = obj1+obj2; cout<<obj3.rep; getch(); return 0; }

12. Define class MaxMin to store a list of numbers and maximum and minimum of the data.test your class with data(-1025,168,-56,0,284,12,7).

LAB ASSIGNMENT C++


13. Define class Temprature which has two datamembers centigrade cTemp and Fahrenheit fTemp.WAP to convert and display temperature using class. 14. WAP to overload area of constructor for calculating circle,square and rectangle.
#include<iostream> using namespace std;

class math { int r; double d;

public: void set_value(int t) { r = t;

} void set_value2(double f) { d=f;

LAB ASSIGNMENT C++


} int area (int ) { return r*r; } int area(double) { return d*d; } };

int main() { int y,u,g,h; char c; math cir,squ; cout<<"Enter the redius:\n";// for the circle cin>>y; cir.set_value(y);

LAB ASSIGNMENT C++


u=cir.area(int); cout<< "area of the circle = " << u; cout << "Enter the side size:\n";// for the square cin>> g; squ.set_value2(g); h=squ.area(double); cout<<" area of the square = " << h; cin>>c; return 0; }

15. Design a constructor for the class BANK A/c Use Data members: 1. Name Of theAccount 2. Account Number 3.Balance A/c Member Function(Constructors) 1) To assign Initial Value 2) To deposit an amount 3) To withdraw an amount after checking balance 4) To display name and balance 16. Design a constructor for the class Employee Use Data members:

LAB ASSIGNMENT C++


2. Name Of Employee 2. Employee Id 3. Dept name 4.salary Member Function(Constructors) 1) To assign Initial Value 2) Calculate salary using formula 3) To display name and salary 17. WAP to use Single Inheritance.
#include<iostream.h> #include<conio.h> class emp { public: int eno; char name[20],des[20]; void get() { cout<<"Enter the employee number:"; cin>>eno; cout<<"Enter the employee name:"; cin>>name; cout<<"Enter the designation:"; cin>>des; } }; class salary:public emp { float bp,hra,da,pf,np; public: void get1() { cout<<"Enter the cin>>bp; cout<<"Enter the cin>>hra; cout<<"Enter the cin>>da; cout<<"Enter the cin>>pf; } void calculate() { np=bp+hra+da-pf; } void display() {

basic pay:"; Humen Resource Allowance:"; Dearness Allowance :"; Profitablity Fund:";

LAB ASSIGNMENT C++


cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf< <"\t"<<np<<"\n"; } }; void main() { int i,n; char ch; salary s[10]; clrscr(); cout<<"Enter the number of employee:"; cin>>n; for(i=0;i<n;i++) { s[i].get(); s[i].get1(); s[i].calculate(); } cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n"; for(i=0;i<n;i++) { s[i].display(); } getch(); }

Output:
Enter the Number of employee:1 Enter the employee No: 150 Enter the employee Name: ram Enter the designation: Manager Enter the basic pay: 5000 Enter the HR allowance: 1000 Enter the Dearness allowance: 500 Enter the profitability Fund: 300 E.No E.name des BP HRA DA PF NP 150 ram Manager 5000 1000 500 300 6200

18. WAP to sort numbers in Ascending order using Single Inheritance. 19. WAP to convert data from class d1 to D2 using type conversion. 20. WAP to use member function.

LAB ASSIGNMENT C++


#include<iostream.h> #include<conio.h> class stat { int code; static int count; public: stat() { code=++count; } void showcode() { cout<<"\n\tObject number is :"<<code; } static void showcount() { cout<<"\n\tCount Objects :"<<count; } }; int stat::count; void main() { clrscr(); stat obj1,obj2; obj1.showcount(); obj1.showcode(); obj2.showcount(); obj2.showcode(); getch(); }

Output:

LAB ASSIGNMENT C++


Count Objects: 2 Object Number is: 1 Count Objects: 2 Object Number is: 2

.
#include<iostream.h> #include<conio.h> class test { int code; static int count; public : void setcode(void) { code= ++count; } void showcode(void) { cout<<"object number"<<code<<endl; } static void showcount(void) { cout<<"count"<<count<<endl; } }; int test::count; int main() { clrscr(); test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); return(1); }

You might also like