220 likes | 329 Views
Classes. Class. An important feature of C++ that supports object-oriented programming(OOP) A class is a collection of data and functions. Function are written to work upon the data items. Classes are used to create user defined data types.
E N D
Class • An important feature of C++ that supports object-oriented programming(OOP) • A class is a collection of data and functions. • Function are written to work upon the data items. • Classes are used to create user defined data types. • Classes contains objects, and objects have the same relationship to class that a variable has to a data type. • When a class is defined, it does not occupy any space in the computer memory. • It only defines the data items and functions.
class Data data1 data2 Functions Funct1() Funct2() Class • Syntax of a class and structure is similar but • Structure is used hold only data, • While classes are used to hold both data and functions.
Declaring a Class • General syntax to declare a class is: class class_name { body of the class }; • Class contains: • data item(data members) • Functions(member functions)
A simple class class abc { private: int a, b; public: void get_value() { cout<<“enter the value of a and b”<<endl; cin>>a>>b; } void print() { cout<<“value of a = “<<a<<endl; cout<<“value of b = ”<<b<<endl; } }; Name of class Data members Member functions
Member Access Specifiers • The commands that determine whether a member of a class can be accessed from outside the class or not. • Key feature of OOP is data hiding • i.e. data is concealed within a class • Two member access speifiers are used: • Private:(data/ functions can only be accessed from within the class) • Public: (data/ functions are accessible from outside the class)
Objects • A data type is used to declare a variable. • A variable of a data type is called instance of that data type. • Each variable follows the rules of its data type. • Space is reserved when the variable of that data type is declared. • A class is also like a data type • Used to declare variable/ instances • These variables or instances of a class are known as objects. • i.e. objects of the class consists of both data member and member functions • Combining both data and function in one unit is called encapsulation
Declaring Objects • The objects of a class are declared in similar way as the variable of any data or structure type are declared. • When class is defined: • No space is reserved • It only provides information of how its objects will look like. • But when objects of that class are declared, memory is reserved for that object. • Syntax: class_name object_name;
Calling member functions • Member functions of a class is called in a similar way as a data item of structure is called. • Using dot operator also called class member access operator • Syntax: Object_name.member_function(with arguments if any);
Example # 1 void main() { clrscr(); my_class x; // object of class x.get(); x.print(); getch(); } Output Value of a = Value of b = #include<iostream.h> #include<conio.h> class my_class { private: int a, b; public: void get() { cout<<“enter a value of a & b”<<endl; cin>>a>>b; } void print() { cout<<“Value of a = <<a<<endl; cout<<“Value of b = <<b<<endl; } };
Example # 2 member function with argument #include<iostream.h> #include<conio.h> class abc { private: int x; public: void set_data(int d) { x = d; } void show() { cout<<“ value is = ”<<x<<endl; } }; void main() { abc s1, s2; //objects s1.set_data(23); s2.set_data(15); s1.show(); s2.show(); getch(); }
Write a class with tag date, with three data members date, month and year, input values from user using one member functions and another member function to display the result. • Write a class with tag student to input the name of student, marks of three subjects, calculate the total marks and average marks. • Write a class with tag parts, having three data member( model_no, part_no, and price), use a member function to set value in the data members using arguments and another member function to display the values.
Member function outside the class • When member function is declared outside the class, • only the prototype of the member function is declared inside the class • Member functions are declared in the similar way as a user-defined function are defined. • Using scope resolution operator (::) • It used with the function declarator • Syntax : type class_name :: function_name(arguments if any) { body of the function }
Example #3 member function outside the class void my_class::get() { cout<<“enter a value of a & b”<<endl; cin>>a>>b; } void my_class::print() { cout<<“value of a = <<a<<endl; cout<<“value of b = <<b<<endl; } Output Value of a = Value of b = #include<iostream.h> #include<conio.h> class my_class { private: int a, b; public: void get(); void print(); }; void main() { clrscr(); my_class x; // object of class x.get(); x.print(); getch(); }
Constructors • A constructor is a member function of a class: • that is called automatically when an object of that class is created. • Has same name as the name of the class. • May have arguments but it can not return any value
Example #4a constructor #include<iostream.h> #include<conio.h> class abc { public: abc() { cout<<“this is constructor”<<endl; } void print() { cout<<“exit”<<endl; } }; void main() { clrscr(); abc a, b, c; getch(); }
Example #4b constructor void main() { clrscr(); counter c1, c2; cout<<“c1 = ”<<c1.get_count()<<endl; cout<<“c2 = ”<<c2.get_count()<<endl; c1.inc_count(); //increment c1 c2.inc_count(); //increment c2 c2.inc_count(); //increment c2 cout<<“c1 = ”<<c1.get_count()<<endl; cout<<“c2 = ”<<c2.get_count()<<endl; getch(); } Output c1 = 0 c2 = 0 c1 = 1 c2 = 2 #include<iostream.h> #include<conio.h> class counter { private: int count; public: counter() { count = 0; } void inc_count() { count++; } int get_count() { return count; } };
Example #4c initializing data using constructor • Constructor function are normally: • used to initialize values in the data members of a class when the program is executed. • Also called automatic initialization
Example #4c constructor #include<iostream.h> #include<conio.h> class sum { private: int n, m, s; public: sum(int x, int y) { n = x; m = y; s = n + m; } void psum() { cout<<“sum is = ”<<s<<endl; } }; void main() { clrscr(); sum a(2,2), b(3,3); a.psum(); b.psum()’ getch(); }
Destructor • When an object is destroyed, a special member function is executed automatically. • Called destructor function or destructor • Has same name as that of a class but has tilde sign (~) before the name of the class. • Destructor do not take any arguments and does not return any value. • Object are destroyed when all the statements of the function in which it is declared are executed. • Destructors are used to free the memory that was allocated for the objects.
Example #5 destructor #include<iostream.h> #include<conio.h> class abc { public: abc() { cout<<“this is constructor”<<endl; } ~abc() { cout<<“this is destructor”<<endl; } }; void main() { clrscr(); abc a; int b=2, c=2; cout<<sum is “<<b+c<<endl; getch(); }
Write a program using class to input values in to an array and find maximum value in the array. The class should contain: • One data member to array of int type • One data member to store the max value • One member function to input data into array • One member function to find max value • One member function to print the max value • Member functions should be described outside the class.