220 likes | 385 Views
OBJECT-ORIENTED PROGRAMMING (OOP). Introduction. C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980’s. C+ + is a superset of C. Almost all c programs are also C++ programs.
E N D
Introduction • C++ is an object-oriented programming language. • It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980’s. • C+ + is a superset of C. • Almost all c programs are also C++ programs
EXAMPLE PROGRAM IN C++ #include <iostream> //header file using namespace std; int main() { cout<<“C++ is better than C\n”; return 0; }
PROGRAM EXPLANATION • <iostream> • This header supports C++ I/O operations. • iostream is to C++ what stdio.h is to C • using namespace std • This tells the compiler to use the std name space • std::cout • This is the namespace in which the entire Standard C++ library is declared • This is required when we use the names that are brought into the program by the processor directive #include<iostream> • int main()
PROGRAM EXPLANATION • cout • An object • Predefined in C++ to correspond with the “standard output stream” • Stream • An abstraction that refers to a flow of data • The standard output stream normally flows to the screen • The Operator << • Directs the contents of the variable on its right to the object on its left. • << is called insertion or put to operator.
INPUT WITH cin int main() { int ftemp; cout<<“Enter temperature in fahrenheit: “; cin>>ftemp; int ctemp=(ftemp-32)*5/9; cout<<“Equivalent in celsius is : “<<ctemp; cout<<endl; return 0; }
cin • An object • Predefined in C++ to correspond with the “standard input stream” • The Operator << • It extracts (or takes) the value from the keyboard and assigns it to the variable on its right. • >> is called extraction or get from operator.
main() • main () returns an integer value to the operating system. • Every main () in C++ should end with a return (0) statement; otherwise a warning an error might occur. • Note that the default return type for all function in C++ is int.
Cascading of I/O Operators • The multiple use of << in one statement is called cascading. • Cout << “Sum = “ << sum << “\n”; • First sends the string “Sum = “ to cout and then sends the value of sum. Finally, it sends the newline character so that the next output will be in the new line. • Cin >> number1 >> number2;
An Example with Class #include<iostream.h> // include header file using namespace std; class person { char name[30]; int age; public: void getdata(void); void display(void); };
void person :: getdata(void) { cout << “Enter name: “; cin >> name; cout << “Enter age: “; cin >> age; } Void person : : display(void) { cout << “\nNameame: “ << name; cout << “\nAge: “ << age; }
int main() { person p; p.getdata(); p.display(); Return 0; } //end of example
The output of program is: Enter Name: Ravinder Enter age:30 Name:Ravinder Age: 30
factors of a number #include<iostream.h> #include<conio.h> void main() { int i,n; clrscr(); cout<<"Enter a number:"; cin>>n; cout<<"\nThe factors of given number are:\n";
for(i=1;i<=n;i++) { if(n%i==0) { cout<<i<<" "; } } getch(); }
Days into year, week and days #include<iostream.h>#include<conio.h>void main(){ clrscr(); int y,d,w; cout<<"Enter No. of days:"; cin>>d;
y=d/365; d=d%365; w=d/7; d=d%7; cout<<"\nYears: "<<y<<"\nWeeks: "<<w<<"\nDays: "<<d; getch();}
Raise to a power • #include<iostream.h>#include<conio.h>#include<math.h> //for pow() functionvoid main(){ clrscr(); int x,n,res; cout<<"Enter value of x and n:"; cin>>x>>n;