1 / 27


Lab#1. Classes. What is a class?. A collection of variables combined with a set of related functions. Object. Member Functions (methods ). Member Variables (data members). Classes in C++. A class definition begins with the keyword class .

Download Presentation


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.


Presentation Transcript

  1. Lab#1 Classes nora albabtin

  2. What is a class? • A collection of variables combined with a set of related functions Object Member Functions (methods) Member Variables (data members) nora albabtin

  3. Classes in C++ • A class definition begins with the keyword class. • The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Any valid identifier Class body (data member + methods) nora albabtin

  4. Access level of the class members • privateand public • the default is private. • Private : • Accessible only to member functions of class • Private members and methods are for internaluse only. • Public: • can be accessed outside the class directly. nora albabtin

  5. Access level of the class members class class_name { private: … … … public: … … … }; private members or methods Public members or methods nora albabtin

  6. Memory AllocationConstructor • Public function member • Has the same name as the class • Has no return value (not even void) • Called and executed each time a new instance of the class is created • Used to set initial values for data members nora albabtin

  7. Memory AllocationDestructor • Has the same name as the class but with (~) • Has no return value, neither arguments • Called and executed each time an object is destroyed. • Used to cleanup the memory after an object is destroyed nora albabtin

  8. Creating Methods You can declare a method two ways. The most common way is to declare a method inside the class declaration and then implement it outside. The second way is to declare and implement the method at the same time inside the class declaration. However, there is special syntax for the method definition. nora albabtin

  9. Example Student::Student(){ stnum=s; }Student::~Student(){cout << "Object has been destroyed" ;} void Student::setstnum(intsn){ stnum = sn; }int Student::getstnum(){ return stnum; } class Student{private:intstnum; public: Student(int s);   void setstnum(intsn);intgetstnum();  ~Student();}; nora albabtin

  10. Object • A class is a type, and an object of this class is just a variable. • You can have many objects of the same class. • Intx ; • StudentS1 ; nora albabtin

  11. Accessing Class Members • Through an object of the class using (.) • StudentS1; • S1.setstnum(1234); nora albabtin

  12. nora albabtin

  13. nora albabtin

  14. nora albabtin

  15. nora albabtin

  16. Exercise#1:Trace the code below #include <iostream> #include <string> using namespace std; class person { private: string name; int age; public: person(); person(string , int ); void set(string,int); string getname(); intgetage(); }; nora albabtin

  17. Exercise#1:Trace the code below person::person() { name="NO Name"; age=0; } person::person(string pn,int pa) { name=pn; age=pa; } nora albabtin

  18. Exercise#1:Trace the code below void person::set(string n, int a) { name=n; age=a; } string person::getname() { return name; } int person::getage() { return age; } nora albabtin

  19. Exercise#1:Trace the code below int main() { person a; person b("Fahad",24); cout<<"Persons information : "<<endl; cout << a.getname() << ": " << a.getage() <<endl; cout << b.getname() << ": " << b.getage() << endl; cout<<"*****************************************"<<endl; a.set("Ahmad",30); b.set("Khaled", 20); cout<<"Persons information after modification : "<<endl; cout << a.getname() << ": " << a.getage() <<endl; cout << b.getname() << ": " << b.getage() << endl; return 0; } nora albabtin

  20. Exercise#2 Define a class Rectangle which contains: • Data members: length and width. • Member functions: • Function area() to compute the area of the rectangle. • Function getdata( ) to prompt the user to enter the length and width for a rectangle. • Function showdata( ) to display length, width and area of a rectangle nora albabtin

  21. Answer of Exercise#2 nora albabtin

  22. For more understanding • Example of class in our life nora albabtin

  23. nora albabtin

  24. nora albabtin

  25. nora albabtin

  26. nora albabtin

  27. nora albabtin

More Related