1 / 21

OBJECT-ORIENTED PROGRAMMING (OOP)

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.

kristy
Download Presentation

OBJECT-ORIENTED PROGRAMMING (OOP)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OBJECT-ORIENTED PROGRAMMING (OOP)

  2. 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

  3. EXAMPLE PROGRAM IN C++ #include <iostream> //header file using namespace std; int main() { cout<<“C++ is better than C\n”; return 0; }

  4. 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()

  5. 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.

  6. 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; }

  7. 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.

  8. 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.

  9. 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;

  10. Structure of C++ Program

  11. 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); };

  12. void person :: getdata(void) { cout << “Enter name: “; cin >> name; cout << “Enter age: “; cin >> age; } Void person : : display(void) { cout << “\nNameame: “ << name; cout << “\nAge: “ << age; }

  13. int main() { person p; p.getdata(); p.display(); Return 0; } //end of example

  14. The output of program is: Enter Name: Ravinder Enter age:30 Name:Ravinder Age: 30

  15. 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";

  16. for(i=1;i<=n;i++) { if(n%i==0) { cout<<i<<" "; } } getch(); }

  17. 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;

  18. y=d/365; d=d%365; w=d/7; d=d%7; cout<<"\nYears: "<<y<<"\nWeeks: "<<w<<"\nDays: "<<d; getch();}

  19. 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;

  20. res=pow(x,n); cout<<"\nResult="<<res; getch();}

More Related