Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.

Similar presentations


Presentation on theme: "Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data."— Presentation transcript:

1 Classes

2  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data members) Object

3  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 { …. }; Class body (data member + methods) Any valid identifier

4  private and public  the default is private.  Private :  Accessible only to member functions of class  Private members and methods are for internal use only.  Public:  can be accessed outside the class directly.

5 class class_name { private: … public: … }; Public members or methods private members or methods

6 ◦ 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

7 ◦ 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

8 class Student { private: int stnum; public: Student(int s); void setstnum(int sn); int getstnum(); ~Student(); }; Student::Student( int s) { stnum=s; } Student::~Student() { cout << "Object has been destroyed" ; } void Student::setstnum(int sn) { stnum = sn; } int Student::getstnum() { return stnum; }

9  A class is a type, and an object of this class is just a variable.variable  You can have many objects of the same class. Int x ;Student S1 ;

10  Through an object of the class using (.)  Through a pointer to the class using (->) Student *S1ptr; S1ptr-> setstnum(1234); Student S1; S1. setstnum(1234);

11 #include using namespace std; class person { private: string name; int age; public: person(); person(string, int ); void set(string,int); string getname(); int getage(); };

12 person::person() { name="NO Name"; age=0; } person::person(string pn,int pa) { name=pn; age=pa; }

13 void person::set(string n, int a) { name=n; age=a; } string person::getname() { return name; } int person::getage() { return age; }

14 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; }

15 Define a class Rectangle which contains:  Data members: length and width.  Member functions: ◦ A constructor with two parameters that assigns values to the data members of the created object. ◦ 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


Download ppt "Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data."

Similar presentations


Ads by Google