slide1
Download
Skip this Video
Download Presentation
Lecture# 22 Programming Concepts

Loading in 2 Seconds...

play fullscreen
1 / 33

Lecture# 22 Programming Concepts - PowerPoint PPT Presentation


  • 91 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
slide1

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

slide10
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 ;
  • } ;
slide13
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