270 likes | 447 Views
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 .
E N D
Lab#1 Classes nora albabtin
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
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
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
Access level of the class members class class_name { private: … … … public: … … … }; private members or methods Public members or methods nora albabtin
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
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
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
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
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
Accessing Class Members • Through an object of the class using (.) • StudentS1; • S1.setstnum(1234); nora albabtin
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
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
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
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
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
Answer of Exercise#2 nora albabtin
For more understanding • Example of class in our life nora albabtin