Вы находитесь на странице: 1из 5

Object-: An object is an identifiable entity with some characteristics and behavior.

Class-: A class represents a group of objects that share common properties, behavior and relationships. Give example of class definition Data Abstraction-: Abstraction refers to act of representing essential features without including the background details or explanations. Give example of class definition Encapsulation-: The wrapping up of data and associated functions into a single unit is known as ENCAPSULATION. Encapsulation implements data abstraction. Give example of class definition class enforce data-hiding, abstraction & encapsulation A class groups its members into three sections : private, protected, and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class enforces data-hiding. The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. Abstraction means representation of essential features without including the background details and explanation. Classes & Object An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire: private members of a class are accessible only from within other members of the same class. protected members are accessible from members of their same class ,but also from members of their derived classes. Finally, public members are accessible from anywhere where the object is visible. By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access.
Syntax class <class name> { private: Data members ; member functions; public:

Data members ; member functions; protected: Data members ; member functions;}; 1. Class definition ends with a ; and may contains function prototypes/definition and variables. 2. Size of a class can be calculated on the basis of summing up the size of all data types defined with in the class, any section. 3. Member functions / data members defined in public section accessible by non member functions (main() )through object. 4. There are two methods to define a function : a. Function definition inside a class When function body has one or two line code , you can define function inside the class. It will become automatically inline i.e. its code gets expanded at the place of its function call. It reduces calling Overheads but if code is long , increases code length. b. Function definition outside the class A function can be defined outside the class using following syntax : Return type <Class name > :: function name () { body of function} 5. Global Class When a class is defined above all function definitions is called Global Class. Object of this class can be Local or Global. 6. Local Class When a class is defined inside a function or a block is called Local Class. Object of this class can only be local. 7. Object can be assigned to another object but cannot be compared. 8. No initialisation is allowed inside the class, it can be performed using functions. 9. A member function can access all the three sections of the class. 10. Nesting of Member Function When a member function calls another member function is called Nesting. 11. Containership : When a class contains object of another class is called Containership. It is also called HAS-A relationship. Example : class person { int no; char sname[40]; public: void getdata(); void putdata();}; class students { person p; Containership char add[40]; public: void getp(); void putp() { p.putdata(); cout<<add; }; 12. Static Data Members are like global variable for its class. These are class variables and accessed with class name and :: . this variable is declared in the class and must defined outside the class.

13. Static Member Functions : These functions can only access static member functions. 14. Array of object can be declared as <class name > <arrayname> [Index] SAMPLE QUESTION 1. Define a class student with the following description: Private members: Rollno,name,marks in five subjects, average and remarks A function calavg() which calculates average on the basis of marks obtained in five subjects. A function assignrem() which assigns remark as Selected if average is more than 40 otherwise set remark as NOT SELECTED.

Public Members void getdata() to accept data void putdata() to display all records float retavg() returns average char *ret_rem() - return remarks; void chgavg(int n) increase average marks by n if average is less than 40. Create 10 objects of the class student. Accept the records. Display the records. Display the records for the students who are selected. Modify the average by a number n if average is less than 40. Display the modified array.

2. 3. 4. 5. 6. 7.

class student { private: int rno; char name[40]; int m[5]; float avg; char grade; char remarks[40]; void calavg(); void assignrem(); public: void getdata(); void putdata(); float retavg() { return avg; }

char *ret_rem() { return remarks;} void chgavg(int n) { avg=avg+n; assignrem();} }; // Accept data void student::getdata() { cout<<enter Rollno. ; cin>>rno; cout<<enter Name ; gets(name); for(int i=0;i<5;i++) { cout<<Enter marks ; cin>>m[i]; } calavg(); assignrem(); } Void student::assignrem() { If(avg<40) strcpy(remarks,Not Selected); else strcpy(remarks,Selected); } void student::calavg() { int t=0; for(int i=0;i<5;i++) t= t+m[i]; avg=(float) t/5; } Void putdata() { cout<<rno<<\n<<name ; for(int i=0;i<5;i++)

, cout<<\n marks <<m*i+;cout<<\n avg <<avg; cout<<\n <<remarks; void main() { Student s[10]; for(int i=0;i<10;i++) s[i].getdata();

for(i=0;i<10;i++) s[i].putdata(); getch(); for(int i=0;i<10;i++) if(strcmpi(s*i+.ret_rem(), Selected)==0) s[i].putdata(); for(int i=0;i<10;i++) { If(s[i].retavg()<40) s[i].chgavg(5); } for(i=0;i<10;i++) s[i].putdata(); getch(); }

Вам также может понравиться