340 likes | 466 Views
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 ;
E N D
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 ; • s1.course ; • s1.age ; • s1.year ;
Accessing structure members • cout << s1.name ; • cout << s1.course ; • cout << s1.age ; • cout << s1.year ;
Structure • Student stdnt1 , stdnt2 ; • stdnt1 + stdnt2 ; //Wrong • Student s1 , s2 ; • s1 = s2 ;
Pointers to Structure • Student *sPtr , s1 ; • sPtr = &s1 ; • *sPtr.name ; Wrong • *sPtr ->name ; • Same as • s1.name
Arrays of Structure • Student s [ 100 ] ; • s [ 0 ].name ; • s [ 1 ].name ; • s [ 2 ].name ; • . . . • s [ 99 ].name ;
Passing Structures to Functions Call by value Call by Reference X
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; • }
Structure • Simple Variable of type • Structure Pointer to Structure • Arrays of Structures • Function that returns a Structure • Pass the Structure to functions
Example 3 • struct Student { • char firstName [ 30 ] ; • char lastName [ 30 ] ; • char course [ 15 ] ; • char rollNo [ 10 ] ; • int age ; • float gpa ; • } ;
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 ; • }
Class • A Class is a user defined data type. Object • The instances of the class are called Objects.
Structure of a class class name_of_class { // definition of a class };
Example 1 • struct Date { • int day ; • int month ; • int year ; • } ; • void main (){ • Date mydate ; • mydate.month = 1 ; • mydate.day = 21 ; • mydate.year = 1979 ; • }
Example 2 • class Date { • int day ; • int month ; • int year ; • } ;
Example 2 • class Date { • int day ; • int month ; • int year ; • } ; • void main ( ) { • Date mydate; • mydate.month = 10 ; // Error • }
Access Specifiers • private • public • Default visibility of all data and function inside a class is private
Complete Structure • class Date { • private : • // private data and functions • public : • // public data and functions • };
Example • class Date { • private : • int day , month , year ; • public : • setMonth ( ) ; • print ( ) ; • };
Example • int main ( ) { • Date mydate ; • mydate.setMonth ( ) ; • mydate.print ( ) ; • }
Example • class Date { • public : • void display ( ) ; • Date ( int day , int month , int year ) ; • private: • int day , month , year ; • } ;
Class Functions • void Date :: display ( ) • { • cout << day << “/ " << month << “/ " << year ; • } • Scope Resolution Operator
Example 3 • int main ( ) { • Date mydate ; • mydate.display ( ) ; • }
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 ; • } ;
Example 3 • void Date :: setDay ( int i ) • { • day = i ; • }
Example 3 • int main ( ) { • Date mydate ; • mydate.setDay ( 10 ) ; • }
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 }
Example 3 • int main ( ) { • Date mydate ( 1 , 1 ,2002 ) ; • mydate.display ( ) ; • } • Date :: Date ( int day , int month , int year = 2002 )
Example 3 • main ( ) { • Date mydate ( 1 , 1 ,2002 ) ; • Date mydate ( 1 , 1 ) ; • }