Found 35502 Articles for Programming

Access Modifiers in C++

George John
Updated on 24-Jun-2020 11:34:49

1K+ Views

Access Modifiers are used to implement data hiding in object oriented programming. There are three types of access modifiers used in C++. These are public, private and protected. Details about these are given as follows.Public Access ModifierThe data members and member functions in a class that are declared public are available to everyone, including other classes. They can be accessed from any place in the program using the dot operator with the class object.A program that demonstrates public access specifier is given as follows.Example Live Demo#include using namespace std; class Add {    public:    int a, b;    void sum() ... Read More

Default Constructors in C++

Ankith Reddy
Updated on 24-Jun-2020 11:35:35

14K+ Views

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors.Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.A program that demonstrates default constructors is ... Read More

Destructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:36:56

15K+ Views

Destructors in C++ are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc.Destructors are different from normal member functions as they don’t take any argument and don’t return anything. Also, destructors have the same name as their class and their name is preceded by a tilde(~).A program that demonstrates destructors in C++ is given as follows.Example Live Demo#include using namespace std; class Demo {    private:    int num1, num2;    public:    Demo(int n1, ... Read More

Copy Constructor in C++

Chandu yadav
Updated on 24-Jun-2020 11:38:16

13K+ Views

The copy constructor is a type of constructor. It creates an object and initializes it with an object of the same class. If the copy constructor is not defined in the class, the compiler itself defines one. A copy constructor is a must for a class that has pointer variables or dynamic memory allocations.A program that demonstrates a copy constructor is as follows.Example Live Demo#include using namespace std; class Demo {    private:    int num1, num2;    public:    Demo(int n1, int n2) {       num1 = n1;       num2 = n2;    }    Demo(const ... Read More

Constructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:39:55

8K+ Views

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class.The two main types of constructors are default constructors and parameterized constructors. Details about these are given as follows.Default ConstructorsDefault constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are ... Read More

Nested Classes in C++

Ankith Reddy
Updated on 24-Jun-2020 11:40:43

21K+ Views

A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.A program that demonstrates nested classes in C++ is as follows.Example Live Demo#include using namespace std; class A {    public:    class B {       private:       int num;       public:       void getdata(int n) {          num = n;       }       void putdata() {          cout

Local Class in C++

George John
Updated on 24-Jun-2020 11:23:17

2K+ Views

A class declared inside a function is known as a local class in C++ as it is local to that function.An example of a local class is given as follows.#include using namespace std; void func() {    class LocalClass {    }; } int main() {    return 0; }In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.A local class name can only be used in its function and not outside it. Also, the methods of a local class must be defined inside it only. ... Read More

Encapsulation in C++

Ankith Reddy
Updated on 24-Jun-2020 11:24:18

1K+ Views

Encapsulation brings together the data and the methods that manipulate the data into a single component and protects them from outside interference. In essence, encapsulation involves bundling the data as well as the functions that use the data. Data encapsulation in lead to the very important concept of data hiding.Encapsulation in C++ is implemented using classes that are user defined data types. These classes contain data types as well as methods that are bound together.A program that represents encapsulation in C++ using classes is given as follows.Example Live Demo#include using namespace std; class EncapsulationDemo {    private:    int length, ... Read More

Abstraction in C++

Arjun Thakur
Updated on 24-Jun-2020 11:25:15

1K+ Views

Abstraction involves providing only the pertinent information to the outside world and hiding the background details. It relies on the separation of interface and implementation for programming.Classes provide abstraction in C++. They provide public methods for the outside world to manipulate data and keep the rest of the class structure to themselves. So the users can use the class as required without knowing how it has been implemented internally.A program to implement abstraction in C++ using classes is given as follows.Example Live Demo#include using namespace std; class Abstraction {    private:    int length, breadth;    public:    void setValues(int ... Read More

Classes and Objects in C++

Chandu yadav
Updated on 24-Jun-2020 11:26:13

4K+ Views

Classes are the prime features of C++ as they support OOPS concepts and are user defined data types. Classes provide the specification for an object and contain data variables as well as functions to manipulate the data in a single package.Class DefinitionsA class definition starts with the keyword class and then the class name. After that the class body is defined. It is enclosed by curly braces. A class definition should either contain a semicolon or a list of definitions after it.An example of a class definition in C++ is as follows.class student {    int rollno;    char name[50]; ... Read More

Advertisements