Download presentation
Presentation is loading. Please wait.
Published byTobias Williamson Modified over 9 years ago
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 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.