1 / 5

Classes

Classes. Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism for creating user-defined data types . Once you create a class type, you can declare one or more objects of that class type

Télécharger la présentation

Classes

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.

E N D

Presentation Transcript


  1. Classes Sujana Jyothi C++ Workshop Day 2

  2. A class in C++ is an encapsulation of data members and functions that manipulate the data. • A class is a mechanism for creating user-defined data types. • Once you create a class type, you can declare one or more objects of that class type • Example: class X { /* define class members here */ }; int main() { X xobject1; // create an object of class type X X xobject2; // create another object of class type X }

  3. Function members in classes: Functions declared inside a class can be any of the following types: Ordinary member functions Constructors Destructors class Example_class //Sample Class for the C++ Tutorial    {       private:         int x; //Data member          int y; // Data member        public:          Example_Class() //Constructor for the C++ tutorial          {              x = 0;             y = 0;         }       ~Example_Class() //destructor for the C++ Tutorial        { }       int Add()  //ordinary member function      {          return x+y;      }};

  4. Access level: • The classes in C++ have 3 important access levels. Private, Public and Protected. Private:    The members are accessible only by the member functions or friend functions. Protected:    These members are accessible by the member functions of the class and the classes which are derived from this class. Public: Accessible by any external member.

  5. // classes example #include <iostream> using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; }

More Related