Lecture# 22
Download
1 / 33

Lecture# 22 Programming Concepts - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

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 ;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture# 22 Programming Concepts' - xanto


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Lecture# 22

Programming Concepts


Structure definition
Structure definition

  • struct Student {

  • char name [ 60 ] ;

  • char address [ 100 ] ;

  • double gpa ;

  • } ;

Structure

name

Keyword


Structures
Structures

  • Student a,b,c;

  • Student s [ 100 ] ;

  • Student *sPtr ;


Initializing structures
Initializing Structures

  • s1.name ;

  • s1.course ;

  • s1.age ;

  • s1.year ;


Accessing structure members
Accessing structure members

  • cout << s1.name ;

  • cout << s1.course ;

  • cout << s1.age ;

  • cout << s1.year ;


Structure
Structure

  • Student stdnt1 , stdnt2 ;

  • stdnt1 + stdnt2 ; //Wrong

  • Student s1 , s2 ;

  • s1 = s2 ;


Pointers to structure
Pointers to Structure

  • Student *sPtr , s1 ;

  • sPtr = &s1 ;

  • *sPtr.name ; Wrong

  • *sPtr ->name ;

  • Same as

  • s1.name


Arrays of structure
Arrays of Structure

  • Student s [ 100 ] ;

  • s [ 0 ].name ;

  • s [ 1 ].name ;

  • s [ 2 ].name ;

  • . . .

  • s [ 99 ].name ;


Passing structures to functions
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;

  • }


Structure1
Structure

  • Simple Variable of type

  • Structure Pointer to Structure

  • Arrays of Structures

  • Function that returns a Structure

  • Pass the Structure to functions


Example 3
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
Class

  • A Class is a user defined data type.

    Object

  • The instances of the class are called Objects.


Structure of a class
Structure of a class

class name_of_class {

// definition of a class

};


Example 1
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
Example 2

  • class Date {

  • int day ;

  • int month ;

  • int year ;

  • } ;


Example 21
Example 2

  • class Date {

  • int day ;

  • int month ;

  • int year ;

  • } ;

  • void main ( ) {

  • Date mydate;

  • mydate.month = 10 ; // Error

  • }


Access specifiers
Access Specifiers

  • private

  • public

  • Default visibility of all data and function inside a class is private


Complete structure
Complete Structure

  • class Date {

  • private :

  • // private data and functions

  • public :

  • // public data and functions

  • };


Example
Example

  • class Date {

  • private :

  • int day , month , year ;

  • public :

  • setMonth ( ) ;

  • print ( ) ;

  • };


Example1
Example

  • int main ( ) {

  • Date mydate ;

  • mydate.setMonth ( ) ;

  • mydate.print ( ) ;

  • }



Example2
Example

  • class Date {

  • public :

  • void display ( ) ;

  • Date ( int day , int month , int year ) ;

  • private:

  • int day , month , year ;

  • } ;


Class functions
Class Functions

  • void Date :: display ( )

  • {

  • cout << day << “/ " << month << “/ " << year ;

  • }

  • Scope Resolution Operator


Example 31
Example 3

  • int main ( ) {

  • Date mydate ;

  • mydate.display ( ) ;

  • }


Example 32
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 33
Example 3

  • void Date :: setDay ( int i )

  • {

  • day = i ;

  • }


Example 34
Example 3

  • int main ( ) {

  • Date mydate ;

  • mydate.setDay ( 10 ) ;

  • }


Constructor
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 35
Example 3

  • int main ( ) {

  • Date mydate ( 1 , 1 ,2002 ) ;

  • mydate.display ( ) ;

  • }

  • Date :: Date ( int day , int month , int year = 2002 )


Example 36
Example 3

  • main ( ) {

  • Date mydate ( 1 , 1 ,2002 ) ;

  • Date mydate ( 1 , 1 ) ;

  • }


ad