Sie sind auf Seite 1von 57

INTRODUCTION TO C++:C++ is a developed in 1983 at AT & T Bell Labs, New Jersy, USA. It is standard version of C language.

C Language is Middle Level Language but C++ is a High Level Language. We write all instruction of C++ in English like Language. So that is why we can say its a High Level Language. It can Object Oriented Programming Language (OOPs). Because all the program procedure or instruction, run or execute with the help of object. Object is anything in a Real Word. It may be one character or multiple characters. It is a case sensitive Language. Because it can only used small letters for execution of program. There is difference between a & A. C Language is advance version with uses of classes. Whenever, we join another function like Polymorphism & other operations like Operator Over Loading. C Language with classes becomes C++ Language.

CONTROL STATEMENTS:In the term software or computer programming, it has a set of instructions called Program. These instructions are also called Statements, which appear sequentially or in either conditional way or in the iterative way to handle such types of statements, some flow control is required. These flow controls are called Control Statements. In order words, the control statements are used to control the cursor in a program according to the condition or according to the requirement in a loop. Further we can say, to change the order of flow control, control statements are required. There are mainly three types of control statements or flow controls. These are illustrated as below: 1. Branching 2. Looping 3. Jumping

CONTROL STATEMETS
BRANCHING
Simple If If-Else Ladder If-Else Nested If Else Switch

LOOPING
For While Do-While

JUMPING
Goto Back Continue

OPERATORS:All operators produce a value from their operands. From every operators, C++ expression can be defined with the help of operators we can solve or compute a formula, compare two expressions or variable values, create logical relationship between compound statements, solve a conditional expression, do low-level programming and special types of operations. There is much type of operators: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment/Decrement operators 6. Conditional operators 7. Bitwise operators

1. Arithmetic Operations:- There are used for mathematical


operations such as addition, subtract, multiplication, division & mode (+,-,*,/,%).

2. Relational Operations:- It is used for comparison between two


operands. We can use the relation & equality operator such as greater, less, greater than equal to, less than equal to, equivalence, not equal to (<,>,<=,>=,==,!=).

3. Logical Operators:- It is used for combine two relational


operators by using AND, OR, NOT operators such as (&&, ||, !). If we are combine two relational expressions by using logical operator than we say that COMPOUND RELATIONAL EXPRESSION. Example: (a>b) && (a>c).

4. Assignment Operators:- It is used to assign the value or copy the


value from right to left hand side. It is used for symbol =.

5. Increment/Decrement Operators:- It is used for increment &


decrement value b one. It is also called COUNTER OPERATOR. Because it can increment or decrement by one. It is of two types:

A. Post Fix Increment/Decrement Operator:- Firstly, we assign


the value after it can be value increment/decrement (++,--) by 1. Example: a++/a--.

B. Pre Fix Increment/Decrement Operator:- First of all,


increment the value for output. Example: ++a/--a.

6. Conditional Operators:- It is used for comparison purpose or it


can use instead of if-else statement. The symbol of conditional operator is ?: exp1?exp2:exp3; If exp1 is true then exp2 will be executed if it is false then exp3 will be executed.

7. Bitwise Operators:- It is performing Boolean algebra on the


corresponding bits in the arguments to produce the result. These operators are used for the manipulation of binary data & also used for addition, multiplication, shifting & negating the binary data. The six types of bitwise operator are: A. Bitwise AND & B. Bitwise OR | C. Bitwise exclusive OR ^ D. Bitwise left << E. Bitwise right >> F. Bitwise NOT ~

MANIPULATOR FUNCTIONS:Manipulator Functions are special stream functions that change certain characteristics of the input and output. These are used to change the format flags and different values for a stream. All the set manipulators are included in the <iomanip.h> header file. All the manipulators are included in stream.h. Some commonly used predefined set manipulators are: 1. endl 2. setw 3. hex, dec, oct 4. setbase 5. setfill 6. setprecision 7. ends 8. ws 9. flush

1. endl:- This manipulator is used in output statements like cout


mainly. This manipulator moves the cursor to next line. The general format is: <<endl;

2. setw( ):- This is an output manipulator. It stands for set width. The
purpose of this manipulator is to display the minimum number of character locations which a variable will take place. The gernal format is: Setw(int w); Where w is width.

3. hex, dec and oct:- Hex is used to convert any octal or decimal
number to Hexadecimal number. Dec is used to convert any octal or hexadecimal number to decimal number. Oct is used to convert any decimal and hexadecimal number to octal number.

4. setbase:- This is used to convert the base of one numeric value to


another base value . it works similar to oct, dec & hex set manipulators. The dec has base value 10, oct has base value 8 & hex has base value 16. the general syntax is: Setbase (base value);

5. setfill:- It is used for fill blank spaces with particular character. It


works with setw( ) set manipulator. The general syntax is: Setfill(char);

6. setprecision:- It is used to display the number of decimal digit in a


number i.e. it will display a floating point value. The general syntax is : Setprecision (p); Where p is a integer value. The maximum & default precision is 6.

7. ends:- This set manipulator is used with \ escape sequence. It will


set end of the string by taking null character.

8. ws:- This manipulator stores the string or data up to first blank


space. This is used in reading statement cin.

9. flush:- When there is no need of string or numeric data in a


variable, then we can apply flush manipulator to clean the memory variable.

CLASSES:A class is a way to bind the data describing an entity and its associated function together. Another definition about class is as: A class is the implementation of an abstract data type (ADT). It defines attributes & methods which implement the data structure & operations of the ADT, respectively. A class is a data type whose variables are objects. Object as a variable has member functions as well as the ability to hold data values. A class is specified just like a structure with a name. The class is specified by keyword class. The three accesses specifiers are:

1. Private:- In the private access specifier, a data member can be


accessed and processed by the member function and the friend function of this class. These can be use to read or write private data members. Note that private member can not be accessed by the outer class or outside this class.

2. Public:- In the public access specifier, data members can be


accessed and processed by all the member functions and the friend function by the other classes or outside this class. It is also called a member function interface which is used to outside class. So any member function or friend function can send messages to an object of this class through these methods pr interface functions. Note that the public data member can always read and write the data outside the functions from any location.

3. Protected:- In the protected access specifier, the data members


can be accessed and processed by the member function and the friend function of the class in which these are declared. These functions can be accessed by the member functions and friend function derived from this class. Note that protected data member and member function is not accessible outside the class. The working of the protected access specifier is similar to the private access specifier. Note that public, private & protected access specifiers are followed by a special symbol colon (:).

DATA HIDING:Data hiding means visibility of members like private, public and protected. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world, & only those functions which are wrapped in the class can access it. These functions provide the interface between the objects data & the program. This insulation of the data from direct access by the program is called DATA HIDING.

1. Private:- In the private access specifier, a data member can be


accessed and processed by the member function and the friend function of this class. These can be use to read or write private data members. Note that private member can not be accessed by the outer class or outside this class.

2. Public:- In the public access specifier, data members can be


accessed and processed by all the member functions and the friend function by the other classes or outside this class. It is also called a member function interface which is used to outside class. So any member function or friend function can send messages to an object of this class through these methods pr interface functions. Note that the public data member can always read and write the data outside the functions from any location.

3. Protected:- In the protected access specifier, the data members


can be accessed and processed by the member function and the friend function of the class in which these are declared. These functions can be accessed by the member functions and friend function derived from this class. Note that protected data member and member function is not accessible outside the class. The working of the protected access specifier is similar to the private access specifier. Note that public, private & protected access specifiers are followed by a special symbol colon (:).

FUNCTION OVERLOADING:A single function name can be used to handle different number and different types of arguments. This is something similar to a particular word having several different meanings depending on the context. Using a single function name to perform different type of tasks is known as FUNCTION OVERLOADING. C++ enable of same name to be defined as long as these function have different set of parameters (at least as per as parameter type number of parameter & order of type concerned). This capability is called function overloading. When an overloaded function is called C++ compilers select. The proper function by examining the number of type or order of function overloading is used to create several functions of same that perform similar task in different data types. Example: Function in math library is overloaded from different numeric data type. int fn (int,int) int fn (int) int fn (float,float,float) Here, func ( ) is overloaded with three different parameter list. The 1 & 2 is differ by different number of argument and 2 & 3 is differ by type of data type & number of arguments.

OPERATOR OVERLOADING:To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called OPERATOR FUNCTION, which describes the task. The general form of an operator function is: return type class name ::operator op (arg list) Where return type is the type of value returned by he specified operation and op is the operator being overloaded. The op is preceded by the keyword operator. Operator op is the function name. Operator functions must be either member functions or friend functions. A basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators & only one for binary operators. This is because the object used to invoke the member function is passed implicitly & therefore is available for the member function. This is not the case with friend function. Arguments may be passed either by value or by reference. Operator functions are declared in the class using proto types as follows: vector operator+(vector); vector operator-( ); friend vector operator+( vector, vector); friend vector operator-( vector); vector operator- (vector &a); int operator ==( vector); friend int operator==( vector, vector) vector is a data type of a class & may represent both magnitude & direction. Process of overloading involves the following steps: 1. Create a class that defines the data type that is to be used in the operation overloading. 2. Declare the operator function operator op( ) in public part of a class. 3. Define the operator function to implement the required operations.

10

CONSTRUCTORS & DESTRUCTORS:CONSTRUCTOR:- A constructor is a special member function used


for automatic initialization of an object. Constructor is used for initialization value to the member data element of a class. In C++, constructor is also a member of a class & has the same name as that of the class. If u has not defined any constructor, then C++ will automatically define a constructor. Note that constructor functions do not any return value. Constructor is modified outside the class provided the member data are declared public. The general syntax is: Class class-name { Private: Data-type data members; Public: Class-name ( ); //constructor define } Constructors are classified in four categories. These are discussing as below:

1. DEFAULT CONSTRUCTOR:- A constructor that accepts no


parameter or argument is called the default constructor. Note that no object can be created without default constructor.

2. PARAMETERIZED CONSTRUCTOR:- This constructor


works similar to the default constructors by the programmer with default argument. The basic difference is execution speed, which is more in the case of parameterized constructor. The constructor which may take argument or parameters is called parameter constructor. This can be done by passing different values arguments to the constructor function, when the objects are created. The general syntax is: Class class-name { Private: Public: Class-name (argument list)

11

{ } //parameterized constructor };

3. COPY CONSTRUCTOR:- A copy constructor is a constructor,


which used to initialize the value of an object by coping values of another object of the same class. It is a constructor of the form (class name &). The general syntax is: Class-name (class-name or constructor name & object-name);

4. DYNAMIC CONSTRUCTOR:- Allocation of memory to objects


at the time of their construction and at the execution time is a difficult task, but this problem is removed with the help of dynamic constructor. So the value provided at run time is called DYNAMIC CONSTRUCTOR. Te dynamic initialization is very handy to change the values without changing the problem. The memory allocation to objects in the dynamic constructor can be allocated with the help of the new operator. Similarly the allocated memory can be realeased with the help of delete operation.

DESTRUCTOR:- Destructors are used to destroy the objects created


or initialized by constructor when they are no longer required. In orders words destructor are used for reinitializing the objects which are initialized by the constructors. A destructor function also has the same name as that of the class bat it is preceded by ~ (tilde symbol). The general syntax is: ~ class-name ( );

12

//1. WAP TO PRINT THE * VERTICALLY UPTO 5 TIMES//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int i; cout<<"Five Stars Vertically"<<endl; for(i=1;i<=5;i++) { cout<<"*"; cout<<endl; } getch(); }

OUTPUT:

13

//2.WAP TO FIND OUT THE AGVERGE OF N NUMBERS USING ARRAY//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,i,sum=0; int a[10]; float avg; cout<<"Enter the value of n = "; cin>>n; cout<<"Enter the elements "<<endl; for(i=1;i<=n;i++) { cin>>a[i]; } for(i=1;i<=n;i++) { sum=sum+a[i]; } avg=sum/n; cout<<Sum is = <<sum<<endl; cout<<"Average of n numbers is "<<avg<<; getch(); }

OUTPUT:

14

//3.WAP TO FIND OUT THE AVERAGE OF N NUMBERS USING DO-WHILE LOOP//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,sum=0,avg,m; cout<< "Enter the no. of terms for avg = "; cin>>n; m=n; do { sum=sum+n; n--; } while(n>0); cout<<"Sum Is "<<sum<<endl; avg=sum/m; cout<< "Average of "<<m<<" Numbers is = "<<avg; getch(); }

OUTPUT:

15

//4.WAP TO FIND OUT THE AVG OF EVERY STUDENT IN CLASS//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int mst1,mst2,mst3,i,a; float avg; char n[10]; cout<<"Enter the number of students in class = "; cin>>a; cout<<endl; for(i=1;i<=a;i++) { cout<<"Enter the name of the student = "; cin>>n; cout<<"Enter the marks of Ist Mst = "; cin>>mst1; cout<<"Enter the marks of 2nd Mst = "; cin>>mst2; cout<<"Enter the marks of 3rd Mst = "; cin>>mst3; avg=(mst1+mst2+mst3)/3; cout<<"Average Per Year of the "<<n<<" is = "<<avg<<endl; cout<<endl; } getch(); }

OUTPUT:

16

//5.WAP TO ILLUSTRATE THE CONCEPT OF DESTRUCTOR//


#include<iostream.h> #include<conio.h> int count=1; class student { public: student() { int count; } void display() { ++count; cout<<"Working of Constructor "<<count<<endl; } ~student() { count--; cout<<"Working of Destructor "<<count; } }; void main() { clrscr(); student stu; stu.display(); getch(); }

OUTPUT:

17

//6. WAP THAT READS RADIUS OF CIRCLE AND PRINT DIAMETER,CIRCUMFERENCE AND AREA USING VALUE OF PI=3.14.//
#include<iostream.h> #include<conio.h> #define pi 3.14 void main() { clrscr(); int rad,dai; float cirm,area; cout<<"Enter the radius of Circle = "; cin>>rad; dai=rad*2; cirm=2*pi*rad; area=pi*rad*rad; cout<<"Daimeter of the Circle is = "<<rad<<"*2 = "<<dai<<endl; cout<<"Circumference of the Circle is = <<"2*"<<pi<<"*"<<rad<<"="<<cirm<<endl; cout<<"Area of the Circle is = "<<pi<<"*"<<rad<<"*"<<rad<<"="<<area; getch(); }

OUTPUT:

18

//7. WAP TO CHECK ENTERED NO. IS EVEN, ODD AND PRIME ALSO FIND OUT THE FACTORIAL OF ENTERED NUMBER//
#include<iostream.h> #include<conio.h> void main() { clrscr(); int num,rem,rem1,i,k; cout<<"Enter Any Number = "; cin>>num; rem=num%2; if(rem==0) { cout<<"Entered Number "<<num<<" is Even "<<endl; for(i=2;i<=num;i++) { rem1=num%i; if(rem1==0) { k=1; } else { } } } if(k==1) { cout<<cout<<"Entered Number "<<num<<" is Composite"<<endl; } else { cout<<"Entered Number "<<num<<" is Prime "<<endl; }

19

int fact=1; for(i=1;i<=num;i++) { fact=fact*i; } cout<<"Factorial Of Entered Number is = "<<fact; getch(); }

Output:

20

//8. WAP TO FIND OUT THE FACTORIAL OF ENTERED NUMBER USING RECURSION//
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; int fact(int); cout<<"Enter Any Number = "; cin>>n; cout<<"Factorial of Entered Number is = "<<fact(n); getch(); } int fact(int n) { int m; if(n==1) { return(1); } else { m=n*fact(n-1); return(m); } }

OUTPUT:

21

//9.WAP TO FIND OUT THE GRADE OF THE STUDENT//


#include<iostream.h> #include<conio.h> void main() { clrscr(); long int total; float per; cout<<"Enter the total marks of the student out of 500 = "; cin>>total; per=(total*100)/500; cout<<"Percentage is = "<<per<<endl; if(per>=80) { cout<<"Student Got A GRADE "<<endl; } else if(per>=60 && per<80) { cout<<"Student Got B GRADE "<<endl; } else { cout<<"Student Got C Grade "<<endl; } getch(); }

OUTPUT:

22

//10.WAP TO FIND OUT THE HEXADECIMAL SUM OF TWO NUMBERS//


#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); int a,b,sum; cout<<"Enter any Two Numbers"<<endl; cin>>a>>b; sum=a+b; cout<<"Hexadecimal Sum Of Entered Two Numbers is = "<<hex<<sum; getch(); }

OUTPUT:

23

//11.WAP TO FIND OUT THE BIGGEST AND SMALLEST NUMBER IN ANY ARRAY//
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[5],i,big,small; cout<<"Enter Any Five Integers"<<endl; for(i=1;i<=5;i++) { cin>>a[i]; } big=a[1]; small=a[1]; for(i=1;i<=5;i++) { if(a[i]>big) { big=a[i]; } } for(i=1;i<=5;i++) { if(a[i]<small) { small=a[i]; } } cout<<"Biggest Number is "<<big<<endl; cout<<"Smallest Number is "<<small; getch(); }

OUTPUT:

24

//12. WAP TO FIND LARGEST OF TWO NUMBERS NOTE THAT YOU MUST ENTER DIGIT ONLY ONCE//
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a=2,num; cout<<"Enter Any Number = "; cin>>num; if(num>a) { cout<<"Entered Number "<<num<<" is Larger than "<<a<<endl; } else { cout<<"Entered Number "<<num<<" is Smaller than"<<a<<endl; } getch(); }

OUTPUT:

25

//13.WAP TO FIND SALARY OF TWO EMP. NOTE THAT BONUS IS 0.5% AND TA IS -1% OF SALARY//
#include<iostream.h> #include<conio.h> void main() { clrscr(); long int emp1,emp2,sal1,sal2,bon1,bon2,ta1,ta2,netsal1,netsal2; cout<<"Enter the Salary Of Ist Employee = "; cin>>emp1; bon1=(emp1*.5)/100; ta1=(emp1*1)/100; netsal1=emp1+bon1-ta1; cout<<"Salary of Employee 1 is = "<<emp1<<endl; cout<<"Bonus on Salary is = "<<bon1<<endl; cout<<"TA on Salary is = "<<ta1<<endl; cout<<"Net Salary is "<<emp1<<" + "<<bon1<<" - "<<ta1<<" = "<<netsal1<<endl<<endl; cout<<"Enter the Salary Of 2nd Employee = "; cin>>emp2; bon2=(emp2*.5)/100; ta2=(emp2*1)/100; netsal2=emp2+bon2-ta2; cout<<"Salary of Employee 1 is = "<<emp2<<endl; cout<<"Bonus on Salary is = "<<bon2<<endl; cout<<"TA on Salary is = "<<ta2<<endl; cout<<"Net Salary is "<<emp2<<" + "<<bon2<<" - "<<ta2<<" = "<<netsal2<<endl<<endl; getch(); }

26

OUTPUT:-

27

//14. WAP TO COMPUTE THE RECORD OF ALL STUDENT IN CLASS//


#include<iostream.h> #include<conio.h> struct student { char name[6]; long int roll_no; long int total_marks; char grade; }stu; void main() { int per,i,n; cout<<"Enter the No. Of Student in the Class = "; cin>>n; for(i=1;i<=n;i++) { cout<<"Enter the Name of the Student = "; cin>>stu.name; cout<<"Enter the Roll No. of the Student = "; cin>>stu.roll_no; cout<<"Enter the Total Marks of the StudentOut of 500 = "; cin>>stu.total_marks; per=stu.total_marks/5; cout<<"Enter the Grade of the Student = "; cin>>stu.grade; cout<<endl; cout<<"The Name of the Student is = "<<stu.name<<endl; cout<<"Roll No. of the Student is = "<<stu.roll_no<<endl; cout<<"Total Marks of the Student is = "<<stu.total_marks<<endl; cout<<"Percentage of the Student is = "<<per<<endl;
28

cout<<"Grade of the Student "<<stu.grade<<endl<<endl<<endl; } getch(); }

is =

OUTPUT:-

29

// 15. WAPTO PEAD ROLL NO. AND FEES TO ILLUSTRATE THE CONCEPT OF DELAULT CONSTRUCTOR WITH DEFAULT ARGUMENTS//
#include<iostream.h> #include<conio.h> int roll_no; long int fees; class student { private: int r; long int f; public: student() { roll_no=r; fees=f; } void getdata() { cout<<"Enter the Roll no of the student = "; cin>>r; cout<<"Enter the Fees of the student = "; cin>>f; } void display() { cout<<"Roll no of the student is = "<<r<<endl; cout<<"Fees of the studnet is = "<<f<<endl; } }; void main() { student ss;
30

ss.getdata(); ss.display(); getch(); }

OUTPUT:-

31

//16. WAP TO DISPLAY DIFFERENT COLOURS BY USING SWITCH STATEMENT//


#include<iostream.h> #include<conio.h> void main() { clrscr(); start: char choice; cout<<"Enter Your Choice For Color = "; cin>>choice; switch(choice) { case 'v': cout<<"Color is Violet "<<endl; break; case 'i': cout<<"Color is Indigo"<<endl; break; case 'b': cout<<"Color is Blue"<<endl; break; case 'g': cout<<"Color is Green"<<endl; break; case 'y': cout<<"Color is Yellow"<<endl; break; case 'o': cout<<"Color is Orange"<<endl; break; case 'r': cout<<"Color is Red"<<endl; break; default:
32

cout<<"Cout Wrong Choice is Entered"<<endl; cout<<"Please Try Again"<<endl; goto start; } getch(); }

OUTPUT:-

33

// 17.WAP IO FIND MARKS OF N STUDENTS IN A CLASS OF THREE SUBJECTS BY USING STRUCTURE//


#include<iostream.h> #include<conio.h> struct student { char name[10]; int p,c,m; int total; }stu; void main() { int n,i,total; cout<<"Enter the Number of Students = "; cin>>n; for(i=1;i<=n;i++) { cout<<"Enter the Name of the Student = "; cin>>stu.name; cout<<"Enter the Marks of Physics = "; cin>>stu.p; cout<<"Enter the Marks Of Chemistry = "; cin>>stu.c; cout<<"Enter the Marks of Maths = "; cin>>stu.m; cout<<endl; total=stu.p+stu.c+stu.m; cout<<"Name of the Student is = "<<stu.name<<endl; cout<<"Marks of Phyics = "<<stu.p<<endl; cout<<"Marks of Chemistry = "<<stu.c<<endl; cout<<"Marks of Maths = "<<stu.m<<endl; cout<<"Total Marks = "<<total<<endl; cout<<endl; } getch();
34

OUTPUT:-

35

//18. WAP TO READ STUDENT DATA TO ILLUSTRATE THE CONCEPT OF UNION//


#include<iostream.h> #include<conio.h> union student { char name[6]; int roll_no; int total_marks; char grade; }stu; void main() { int per,i,n; cout<<"Enter the No. Of Student in the Class = "; cin>>n; for(i=1;i<=n;i++) { cout<<"Enter the Name of the Student = "; cin>>stu.name; cout<<"Enter the Roll No. of the Student = "; cin>>stu.roll_no; cout<<"Enter the Total Marks of the Student Out of 500 = "; cin>>stu.total_marks; per=stu.total_marks/5; cout<<"Enter the Grade of the Student = "; cin>>stu.grade; cout<<endl; cout<<"The Name of the Student is = "<<stu.name<<endl; cout<<"Roll No. of the Student is = "<<stu.roll_no<<endl; cout<<"Total Marks of the Student is = "<<stu.total_marks<<endl; cout<<"Percentage of the Student is = "<<per<<endl;

36

cout<<"Grade of the Student is = <<stu.grade<<endl<<endl<<endl; } getch(); }

OUTPUT:-

37

//19. WAP TO FINDOUT THE HCF OF TWO NUMBERS//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int hcf(int,int); int a,b,d; cout<<"Enter two Numbers "<<endl; cin>>a>>b; if(a>b) { d=hcf(a,b); } else { d=hcf(b,a); cout<<"HCF is:"<<d; } getch(); } int hcf(int x,int y) { int r=1; while(r!=0) { r=x%y; x=y; y=r; } return(x); }

OUTPUT:-

38

//20. WAP TO FINDOUT THE SALARY OF THE EMPLOYEE USING CONDITIONAL CONTROL STATEMENT//
#include<iostream.h> #include<conio.h> void main() { clrscr(); int sal,bp; cout<<"Enter the basic pay = "; cin>>bp; sal=(bp!=1000)?((bp<1000)?4*bp+500:2*bp+1000):2000; cout<<"Salary is = "<<sal; getch(); }

OUTPUT:-

39

//21. WAP TO EXPLAIN THE CONCEPT OF RETURN STATEMENT WITH CONDITION//


#include<iostream.h> #include<conio.h> int x,y; void main() { clrscr(); int add; int sum(int,int); cout<<"Enter the value of x = "; cin>>x; cout<<"Enter the value of y = "; cin>>y; if(x>y) { add=sum(x,y); } cout<<"Sum of x and y is = "<<add; getch(); } int sum(int x, int y) { int z; z=x+y; return(z); }

OUTPUT:-

40

//22.WAP TO ILLUSTRATE THE CONCEPT OF ONE CLASS WITH ONE OBJECT BY TAKING STUDENT DATA//
#include<iostream.h> #include<conio.h> class student { private: long int rn; char name[10]; public: void getdata() { cout<<"Enter the Roll No. = "; cin>>rn; cout<<"Enter the Name = "; cin>>name; } void displaydata() { cout<<"Roll No. is "<<rn<<endl; cout<<"Name is "<<name; } }; void main() { class student stu; stu.getdata(); stu.displaydata(); }

OUTPUT:-

41

//23.WAP TO FIND IF A NO. IS EVEN OR ODD USING THE CONCEPT OF INLINE FUNCTION//
#include<iostream.h> #include<conio.h> inline int even(int n) { return!(n%2); } void main() { clrscr(); int n; cout<<"Enter any Number = "; cin>>n; if(even(n)) { cout<<n<<" is Even"; } else { cout<<n<<" is Odd"; } getch(); }

OUTPUT:-

42

//24. WAP TO MULTIPLICATION OF TWO NUMBER BY USING POINTER TO FUNCTION//


#include<iostream.h> #include<conio.h> void main() { int a,b; void mul(); mul(); getch(); } void mul() { clrscr(); int a,b,mul; int *p, *q; p=&a; q=&b; cout<<"Enter the value of a and b "<<endl; cin>>a>>b; mul=(*p)*(*q); cout<<"MULTIPLICATION IS "<<mul; }

OUTPUT:-

43

//25. WAP TO FIND OUT THE SUM OF TWO MATRICES//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j; cout<<"Enter the No of rows and columns of the matrix 1 "<<endl; cin>>r1>>c1; cout<<"Enter the No. of row and columns of the matrix 2 "<<endl; cin>>r2>>c2; cout<<"Enter the elements of the Ist matrix"<<endl; for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { cin>>a[i][j]; } } cout<<"Enter the elements of the 2nd matrix"<<endl; for(i=1;i<=r2;i++) { for(j=1;j<=c2;j++) { cin>>b[i][j]; } } cout<<"Entered matrix 1 is"<<endl; for(i=1;i<=r1;i++)
44

{ for(j=1;j<=c1;j++) { cout<<"\t"<<a[i][j]; } cout<<endl; } cout<<"Enter the elements of the 2nd matrix"<<endl; for(i=1;i<=r2;i++) { for(j=1;j<=c2;j++) { cout<<"\t"<<b[i][j]; } cout<<endl; } cout<<"Sum of two matrices is"<<endl; for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { c[i][j]=a[i][j]+b[i][j]; } } for(i=1;i<=r1;i++) { for(j=1;j<=c1;j++) { cout<<"\t"<<c[i][j]; } cout<<endl; } getch(); }

45

OUTPUT:-

46

//26. WAP TO OVERLOAD + OPERATOR//


#include <iostream.h> #include <conio.h> struct Complex { Complex( double r, double i ) : re(r), im(i) {} Complex operator+( Complex &other ); void Display( ) { cout << re << ", " << im << endl; } private: double re, im; }; // Operator overloaded using a member function Complex Complex::operator+( Complex &other ) { return Complex( re + other.re, im + other.im ); } int main() { clrscr(); Complex a = Complex( 1.2, 3.4 ); Complex b = Complex( 5.6, 7.8 ); Complex c = Complex( 0.0, 0.0 ); c = a + b; c.Display(); }

OUTPUT:-

47

//27. WAP TO OVERLOAD THE UNARY ++ OPERATOR//


#include<iostream.h> #include<conio.h> class Increment { private: int count ; public: //*****Member Function Definitions****** Increment( ) // Default Constructor { count = 0 ; } Increment(int C) // Constructor with Argument { count = C ; } Increment operator ++ ( ) // Operator Function Definition { count++ ; return Increment(count); } void display(void) // Function for displaying the output { cout << count << endl ; } }; // End of the class definition void main(void) { clrscr(); Increment I1, I2(5), I3, I4 ; cout << "\n Before activating the operator ++( )\n" ;
48

cout << " I1 = " ; I1.display( ); cout << " I2 = " ; I2.display( ); ++I1 ; // I1 and I2 are incremented I2++ ; cout << "\n After activating the operator ++( )\n" ; cout << " I1 = " ; I1.display( ); cout << " I2 = " ; I2.display( ) ; I3 = ++I1 ; /* Here, the objects I1 and I2 are incremented again and their I4 = I2++ ; values are assigned to the objects I3 and I4 respectively */ cout << " I3 = " ; I3.display( ) ; cout << " I4 = " ; I4.display( ) ; }

OUTPUT:-

49

//28. WAP TO FIND OUT THE SUM OF TWO MATRICES WITHIN CLASS//
#include<iostream.h> #include<conio.h> class matrix { private: int a[10][10],b[10][10],c[10][10]; int i,j,r1,c1,r2,c2; public: void getdata() { cout<<"enter number rows & columns of first matrix"<<endl; cin>>r1>>c1; cout<<"enter number of rows & columns of second matrix"<<endl; nitin: cin>>r2>>c2; if(c1!=c2 && r1!=r2) { cout<<"wrong number of rows & columns are entered .enter again"<<endl; goto nitin; } cout<<"enter elements of first matrix"<<endl; for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { cin>>a[i][j];
50

} } cout<<"enter elements of second matrix"<<endl; for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { cin>>b[i][j]; } } } void summatrix() { for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=a[i][j]+b[i][j]; } } } void dispdata() { cout<<"sum of two matrices is"<<endl; for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { cout<<\t<<c[i][j]; } cout<<endl; } } }; void main() { clrscr(); matrix m; m.getdata(); m.summatrix();
51

m.dispdata(); getch(); }

//29. WAP TO DISPLAY STUDENT DATA BY USING THE CONCEPT OF SINGLE INHERITANCE//
#include<iostream.h> #include<conio.h> class cse { protected: int rn; public: void get() { cout<< "Enter roll no of student"<<endl; cin>>rn; } void disp() { cout<<"Roll no of student is "<<rn; } }; class mnk:public cse { protected: char name[20]; public: void get() { cse::get(); cout<< "Enter name of student"<<endl; cin>>name; } void disp() {
52

cse::disp(); cout<<endl; cout<<"Name of student is "<<name; } }; void main() { clrscr(); mnk m; m.get(); m.disp(); getch(); }

OUTPUT:-

53

//30. WAP TO PRINT THE STUDENT DATA BY USING THE CONCEPT OF MULTIPLE INHERITANCE//
#include<iostream.h> #include<conio.h> class cse { protected: long int rn; public: void get() { cout<< "Enter roll no of student"<<endl; cin>>rn; } void disp() { cout<<"Roll no of student is "<<rn; } }; class mnk { protected: char name[20]; public: void get() { cout<< "Enter name of student"<<endl; cin>>name; } void disp() {
54

cout<<endl; cout<<"Name of student is "<<name; } }; class marks:public cse,public mnk { int mas; public: void get() { cse::get(); mnk::get(); cout<< "enter the marks of student"<<endl; cin>>mas; } void disp() { cse::disp(); cout<<endl; mnk::disp(); cout<<endl; cout<< "marks of student are " <<mas; } }; void main() { clrscr(); marks m; m.get(); m.disp(); getch(); }

OUTPUT:-

55

//31. WAP TO REVERSE ARRAY ELEMENTS USING POINTER//


#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],i,n; int *p; cout<<"Enter the value of n ="; cin>>n; cout<<"Enter the elements"<<endl; for(i=1;i<=n;i++) { cin>>a[i]; } cout<<"Array After Reverse "<<endl; for(i=n;i>0;i--) { p=&a[i]; cout<<*p<<endl; } getch(); }

OUTPUT:-

56

57

Das könnte Ihnen auch gefallen