1 / 33

Lecture# 22 Programming Concepts

Lecture# 22 Programming Concepts. Structure definition. struct Student { char name [ 60 ] ; char address [ 100 ] ; double gpa ; } ;. Structure name. Keyword. Structures. Student a,b,c; Student s [ 100 ] ; Student *sPtr ;. Initializing Structures. s1.name ;

xanto
Download Presentation

Lecture# 22 Programming Concepts

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. Lecture# 22 Programming Concepts

  2. Structure definition • struct Student { • char name [ 60 ] ; • char address [ 100 ] ; • double gpa ; • } ; Structure name Keyword

  3. Structures • Student a,b,c; • Student s [ 100 ] ; • Student *sPtr ;

  4. Initializing Structures • s1.name ; • s1.course ; • s1.age ; • s1.year ;

  5. Accessing structure members • cout << s1.name ; • cout << s1.course ; • cout << s1.age ; • cout << s1.year ;

  6. Structure • Student stdnt1 , stdnt2 ; • stdnt1 + stdnt2 ; //Wrong • Student s1 , s2 ; • s1 = s2 ;

  7. Pointers to Structure • Student *sPtr , s1 ; • sPtr = &s1 ; • *sPtr.name ; Wrong • *sPtr ->name ; • Same as • s1.name

  8. Arrays of Structure • Student s [ 100 ] ; • s [ 0 ].name ; • s [ 1 ].name ; • s [ 2 ].name ; • . . . • s [ 99 ].name ;

  9. Passing Structures to Functions Call by value Call by Reference  X

  10. struct Student{ • char name[20]; • int rollno; • }; • void init(Student&); • void main(){ • Student s1; • init(s1); • cout<<s1.name<<" "<<s1.rollno; • getch(); • } • void init(Student &a){ • cout<<"Plz enter the name \n"; • gets(a.name); • cout<<"Plz enter the Roll No \n"; • cin>>a.rollno; • }

  11. Structure • Simple Variable of type • Structure Pointer to Structure • Arrays of Structures • Function that returns a Structure • Pass the Structure to functions

  12. Example 3 • struct Student { • char firstName [ 30 ] ; • char lastName [ 30 ] ; • char course [ 15 ] ; • char rollNo [ 10 ] ; • int age ; • float gpa ; • } ;

  13. Student s [ 10 ] ; • for ( int i = 0 ; i < 10 ; i ++ ) { • cout << “Please enter the student's last name : " ; • cin >> s [ i ].lastName ; • cout << “Please enter the student's first name : " ; • cin >> s [ i ].firstName ; • cout << " Please enter the student's course : " ; • cin >> s [ i ].course ; • cout << " Please enter the student's Roll No. : " ; • cin >> s [ i ].rollNo ; • cout << " Please enter the student's grade : " ; • cin >> s [ i ].grade ; • cout << " Please enter the student's age : " ; • cin >> s [ i ].age ; • cout << " Please enter the student's GPA : " ; • cin >> s [ i ].gpa ; • }

  14. Classes And Objects

  15. Class • A Class is a user defined data type. Object • The instances of the class are called Objects.

  16. Structure of a class class name_of_class { // definition of a class };

  17. Example 1 • struct Date { • int day ; • int month ; • int year ; • } ; • void main (){ • Date mydate ; • mydate.month = 1 ; • mydate.day = 21 ; • mydate.year = 1979 ; • }

  18. Example 2 • class Date { • int day ; • int month ; • int year ; • } ;

  19. Example 2 • class Date { • int day ; • int month ; • int year ; • } ; • void main ( ) { • Date mydate; • mydate.month = 10 ; // Error • }

  20. Access Specifiers • private • public • Default visibility of all data and function inside a class is private

  21. Complete Structure • class Date { • private : • // private data and functions • public : • // public data and functions • };

  22. Example • class Date { • private : • int day , month , year ; • public : • setMonth ( ) ; • print ( ) ; • };

  23. Example • int main ( ) { • Date mydate ; • mydate.setMonth ( ) ; • mydate.print ( ) ; • }

  24. Separation of Interface from the Implementation.

  25. Example • class Date { • public : • void display ( ) ; • Date ( int day , int month , int year ) ; • private: • int day , month , year ; • } ;

  26. Class Functions • void Date :: display ( ) • { • cout << day << “/ " << month << “/ " << year ; • } • Scope Resolution Operator

  27. Example 3 • int main ( ) { • Date mydate ; • mydate.display ( ) ; • }

  28. Example 3 • class Date { • public : • Date ( int month , int day , int year ) ; • void display ( ) ; • setDay ( int ) ; • setMonth ( int ) ; • setYear ( int ) ; • private : • int month , day , year ; • } ;

  29. Example 3 • void Date :: setDay ( int i ) • { • day = i ; • }

  30. Example 3 • int main ( ) { • Date mydate ; • mydate.setDay ( 10 ) ; • }

  31. Constructor • class Date { • public : • Date ( int month , int day , int year ) ; • void display ( ) ; • private : • int month , day , year ; • }; • Date :: Date ( int month , int day , int year ) { // Body of the function }

  32. Example 3 • int main ( ) { • Date mydate ( 1 , 1 ,2002 ) ; • mydate.display ( ) ; • } • Date :: Date ( int day , int month , int year = 2002 )

  33. Example 3 • main ( ) { • Date mydate ( 1 , 1 ,2002 ) ; • Date mydate ( 1 , 1 ) ; • }

More Related