Documente Academic
Documente Profesional
Documente Cultură
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
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
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.
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:
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.
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
11
{ } //parameterized constructor };
12
OUTPUT:
13
OUTPUT:
14
OUTPUT:
15
OUTPUT:
16
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
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
OUTPUT:
22
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
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
OUTPUT:-
31
cout<<"Cout Wrong Choice is Entered"<<endl; cout<<"Please Try Again"<<endl; goto start; } getch(); }
OUTPUT:-
33
OUTPUT:-
35
36
OUTPUT:-
37
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
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
OUTPUT:-
43
{ 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
OUTPUT:-
47
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
OUTPUT:-
56
57