Classes and objects

1,557 views

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,557
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
151
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Classes and objects

  1. 1. Classes And Objects 1. 2. 3. 4. 5. 6. STRUCTURE CLASS CLASS DECLARATION CLASS MEMBERS ARRAY OF OBJECTS PASSING OBJECTS TO FUNCTION Compiled By: Kamal Acharya
  2. 2. STRUCTURE  It is the collection of different types of data under the common name. Syntax: struct StructureName { dataType var1; dataType var2; ………………….; dataType varN; }; Compiled By: Kamal Acharya
  3. 3. Example: struct Student { int Roll; char Name[20]; }; Compiled By: Kamal Acharya
  4. 4.  Declaring Variables StructureName StructureName  Example Student ram; Student s[5]; Compiled By: Kamal Acharya variableName; variableName[10];
  5. 5. Intialization: StructureName VariableName ={ val1, val2,.........,valN}; Example: Student Ram={11,”Ram”}; Compiled By: Kamal Acharya
  6. 6. Processing Structure #include<iostream.h> #include<conio.h> struct student { int roll; char name[20]; }; void main() { student s1; clrscr(); Compiled By: Kamal Acharya cout<<"Enter Roll Number"<<endl; cin>>s1.roll; cout<<"Enter Your Name"<<endl; cin>>s1.name; cout<<"Your Name is "<<s1.name<<endl; cout<<"Your Roll Number is "<<s1.roll; getch(); }
  7. 7. Output Compiled By: Kamal Acharya
  8. 8. Passing Structure to Function #include<iostream.h> #include<conio.h> struct student { int roll; char name[20]; }; void display(struct student s); void main() { student s1={12,"Sita"}; clrscr(); display(s1); Compiled By: Kamal Acharya getch(); } void display(struct student s1) { cout<<"Your Name is "<<s1.name<<endl; cout<<"Your Roll Number is "<<s1.roll; }
  9. 9. Output Compiled By: Kamal Acharya
  10. 10. LIMITATION OF STRUCTURES Struct data type can’t be treated as the built in type. Example: struct complex { int real; int img; }c1,c2,c3; c3= c1+c2; //Illegal  Compiled By: Kamal Acharya
  11. 11.  Do not permit data hiding. Structure members can be directly accessed by structure variables by any function any where Example complex c1; c1.real= 20; c1.img=70; Compiled By: Kamal Acharya
  12. 12. CLASSES  C++ incorporates all the features of structure while avoiding the drawbacks in new user defined data types called Class.  Classes can hold both data and the functions  Classes members are private by default. Compiled By: Kamal Acharya
  13. 13. Class Declaration  Syntax: class className { private: variables declaration; functions declaration; public: variables declaration; functions declaration; }; By: Kamal Acharya Compiled
  14. 14. Creating Objects Example class item { int number; float cost; public: void getdata(int a , float b); void putdata(); }; Compiled By: Kamal Acharya item x; //single object item x[5]; // array  At the time of definition also we can create objects. class item { ……… ……….. }x,z[20];
  15. 15. Accessing Class Members  Syntax: objectName.functionName(arguments); Example x.getdata(100,75.5); Compiled By: Kamal Acharya
  16. 16. Defining Member Function Member Function can be defined in two places: 1. Inside the Class Are inline by default. 2. Outside the Class Syntax: returnType className::functionName(arguments) { function body; } Compiled By: Kamal Acharya
  17. 17. Defining member function inside the class class item { int number; float cost; public: void getdata(int a,float b) { number=a; cost=b; } Compiled By: Kamal Acharya void putdata() { cout<<number; cout<<cost; } };
  18. 18. Defining member function outside the class class item { int number; float cost; public: void getdata(int a,float b); void putdata(); }; Compiled By: Kamal Acharya void item::getdata(int a, float b) { number=a; cost=b; } void item::putdata() { cout<<number; cout<<cost; }
  19. 19. C++ program with class #include<iostream.h> #include<conio.h> class item { int number; float cost; public: void getdata(int a, float b); void putdata() { cout<<"Number:"<<number<<endl; cout<<"Cost:"<<cost<<endl; } }; void item::getdata(int a, float b) { number=a; cost=b; } Compiled By: Kamal Acharya void main() { item x; clrscr(); cout<<"Object x"<<endl; x.getdata(10,2.9); x.putdata(); item y; cout<<"Object y"<<endl; y.getdata(12,4.7); y.putdata(); getch(); }
  20. 20. Output Compiled By: Kamal Acharya
  21. 21. Making Outside Function Inline Class item { ………. public: ………….. void getdata(int a, float b); }; inline void item :: getdata(int a, float b) { function body; } Compiled By: Kamal Acharya
  22. 22. Nesting of member function #include<iostream.h> #include<conio.h> class item { int n,m; public: void getdata(int a, int b); int largest(); void putdata(); }; void item::getdata(int a, int b) { n=a; m=b; } void item::putdata() { cout<<"The largest number is " <<largest(); } Compiled By: Kamal Acharya int item::largest() { if(n>m) return n; else return m; } void main() { item x; clrscr(); x.getdata(20,70); x.putdata(); getch(); }
  23. 23. OUTPUT Compiled By: Kamal Acharya
  24. 24. Memory Allocation for Objects • Each objects has its own separate data items and memory are allocated when the objects are created. • Member function are created and put in the memory only once when class are defined. Compiled By: Kamal Acharya
  25. 25. Compiled By: Kamal Acharya
  26. 26. Static Data Members  It is initialized to zero when first object is created.  Only one copy is created for entire class.  It is visible within the class but its life time is the entire program.  Type and scope of each static member variable must be defined outside the class definition.  They are stored separately rather than as a part of the object. Compiled By: Kamal Acharya
  27. 27. Program to show the static data members #include<iostream.h> #include<conio.h> class item { static int count; int number; public: void getdata(int a) { number=a; count++; } void getcount() { cout<<"Count: "<<count<<endl; } }; int item::count; // REMEMBER THIS Compiled By: Kamal Acharya void main() { item a,b,c; clrscr(); cout<<"Before Reading Data" <<endl; a.getcount(); b.getcount(); c.getcount(); a.getdata(10); b.getdata(20); c.getdata(30); cout<<"After Reading Data" <<endl; a.getcount(); b.getcount(); c.getcount(); getch(); }
  28. 28. OUTPUT Compiled By: Kamal Acharya
  29. 29. Static Member Function  It has access to only other static members(function or variables) declared in the same class.  To declare any function static just put the keyword static in front of the function defination. static returnType functionName(arguments) { function body; }  It can be called using the class name as follows: className :: functionName(arguments); Compiled By: Kamal Acharya
  30. 30. Program to show the work of static function #include<iostream.h> #include<conio.h> class item { static int count; int code; public: void setcode() { code=++count; } void showcode() { cout<<"Object Number: "<<code<<endl; } static void showcount() { cout<<"Count: "<<count<<endl; } }; Compiled By: Kamal Acharya int item::count; void main() { item t1,t2; clrscr(); t1.setcode(); t2.setcode(); item::showcount(); item t3; t3.setcode(); item::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); }
  31. 31. OUTPUT Compiled By: Kamal Acharya
  32. 32. Remember It Won’t Work static void showcount() { cout<<code; //code is not static } Compiled By: Kamal Acharya
  33. 33. Array of Objects  We can create the array of objects as: className variable[x];  To access the individual element we have to take the help of the dot operator. variable[i].function(arguments); Compiled By: Kamal Acharya
  34. 34. Program to demonstrate the array of objects #include<iostream.h> #include<conio.h> class employee { char name[30]; int age; public: void getdata(); void putdata(); }; void employee::getdata() { cout<<"Enter name:"<<endl; cin>>name; cout<<"Enter age:"<<endl; cin>>age; cout<<endl; } void employee::putdata() { cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; } Compiled By: Kamal Acharya void main() { employee manager[2]; int i; clrscr(); for(i=0;i<2;i++) { cout<<"Details of manager"<<i+1<<endl; manager[i].getdata(); } cout<<endl; for(i=0;i<2;i++) { cout<<endl<<"Manager"<<i+1<<endl; manager[i].putdata(); } getch(); }
  35. 35. OUTPUT Compiled By: Kamal Acharya
  36. 36. Passing objects to function Two Ways to pass the objects to the function: 1. Passing by value Entire object is passed is passed to the function. 2. Passing by reference Only the reference to the object is passed. Compiled By: Kamal Acharya
  37. 37. Passing by value #include<iostream.h> #include<conio.h> class test { int a; public: void getdata(int x) { a=x; } void add(test, test); }; void test::add(test x, test y) { cout<<"The sum of the values is: “ <<x.a+y.a<<endl; } Compiled By: Kamal Acharya void main() { test t1,t2,t3; clrscr(); t1.getdata(40); t2.getdata(20); t3.add(t1,t2); getch(); }
  38. 38. OUTPUT Compiled By: Kamal Acharya
  39. 39. Passing By Reference #include<iostream.h> #include<conio.h> class test { int a; public: void getdata(int x) { a=x; } void putdata() { cout<<"a= "<<a<<endl; } void change(test &, test &); }; void test::change(test &x, test &y) { int temp; temp=x.a; x.a=y.a; y.a=temp; } Compiled By: Kamal Acharya void main() { test t1,t2,t3; clrscr(); t1.getdata(40); t2.getdata(20); cout<<"Before Swapping"<<endl; cout<<"In t1"<<endl; t1.putdata(); cout<<"In t2"<<endl; t2.putdata(); t3.change(t1,t2); cout<<"After Swapping"<<endl; cout<<"In t1"<<endl; t1.putdata(); cout<<"In t2"<<endl; t2.putdata(); getch(); }
  40. 40. OUTPUT Compiled By: Kamal Acharya

×