Found 7346 Articles for C++

Why C/C++ variables doesn’t start with numbers

George John
Updated on 26-Jun-2020 13:43:26

2K+ Views

In C/C++, a variable name can have alphabets, numbers and underscore( _ ) character. There are some keywords in C/C++ language, apart from them everything is treated as identifier. Identifiers are the name of variable, constants, functions etc.We can not specify the identifier which starts with a number because there are seven phases of compiler as follows.Lexical AnalysisSyntax AnalysisSemantic AnalysisIntermediate Code GenerationCode OptimizationCode GenerationSymbol TableNone of above supports that a variable starts with a number. This is because the compiler gets confused if is a number or identifier until it reaches an alphabet after the numbers. So the compiler will ... Read More

Calling a member function on a NULL object pointer in C++

Chandu yadav
Updated on 26-Jun-2020 13:47:22

863 Views

A class member function can be called using a NULL object pointer.Note − This is undefined behaviour and there is no guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Demo {    public :    void fun() {       cout fun();    return 0; }OutputThe output of the above program is as follows.This member function is called through Null object pointer.Now, let us understand the above program.The class Demo contains a member function fun(). This function displays ... Read More

Initialization of a normal array with one default value in C++

George John
Updated on 26-Jun-2020 13:49:16

15K+ Views

The entire array can be initialized to zero very simply. This is shown below.int arr[10] = {0};However, it is not possible to initialize the entire array to a non-zero value using the above method. This is shown below.int arr[10] = {5};In the above example, only the first element will be initialized to 5. All others are initialized to 0.A for loop can be used to initialize an array with one default value that is not zero. This is shown below.for(i = 0; i

Which is the fastest algorithm to find prime numbers using C++?

Ankith Reddy
Updated on 26-Jun-2020 13:50:52

3K+ Views

The Sieve of Eratosthenes is one of the most efficient ways to find the prime numbers smaller than n when n is smaller than around 10 million.A program that demonstrates the Sieve of Eratosthenes is given as follows.Example#include using namespace std; void SieveOfEratosthenes(int num) {    bool pno[num+1];    memset(pno, true, sizeof(pno));    for (int i = 2; i*i< = num; i++) {       if (pno[i] == true) {          for (int j = i*2; j< = num; j + = i)          pno[j] = false;       }    }    for (int i = 2; i< = num; i++)    if (pno[i])    cout

Calling class method through NULL class pointer in C++

Arjun Thakur
Updated on 26-Jun-2020 13:51:40

361 Views

A class method can be can be called using a NULL class pointer.Note − This is undefined behaviour and there is not guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Example {    public :    void func() {       cout func();    return 0; }OutputThe output of the above program is as follows.The function is called through Null class pointer.Now, let us understand the above program.The class Example contains a member function func(). This function displays ... Read More

Can main function call itself in C++?

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

1K+ Views

The main() function can call itself in C++. This is an example of recursion as that means a function calling itself. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; int main() {    static int x = 1;    cout

What is the meaning of prepended double colon “::” in C++?

George John
Updated on 26-Jun-2020 13:26:44

6K+ Views

The prepended double colon is also known as the scope resolution operator. Some of the uses of this operator are given as follows.Define a function outside a classThe scope resolution operator can be used to define a function outside a class. A program that demonstrates this is given as follows.Example Live Demo#include using namespace std; class Example {    int num;    public:    Example() {       num = 10;    }    void display(); }; void Example::display() {    cout

Private and Protected Members in C++

Arjun Thakur
Updated on 26-Jun-2020 13:29:23

8K+ Views

A class in C++ has public, private and protected sections which contain the corresponding class members.The private data members cannot be accessed from outside the class. They can only be accessed by class or friend functions. All the class members are private by default.The protected members in a class are similar to private members but they can be accessed by derived classes or child classes while private members cannot.A program that demonstrates private and protected members in a class is given as follows −Example Live Demo#include using namespace std; class Base {    public :    int a = 8; ... Read More

Accessing protected members in a C++ derived class

Chandu yadav
Updated on 26-Jun-2020 13:30:35

17K+ Views

A class in C++ has public, private and protected sections which contain the corresponding class members. Protected members in a class are similar to private members as they cannot be accessed from outside the class. But they can be accessed by derived classes or child classes while private members cannot.A program that demonstrates accessing protected data members in a derived class in C++ is given as follows −Example Live Demo#include using namespace std; class Base {    protected :    int num = 7; }; class Derived : public Base {    public :    void func() {       cout

How to “return an object” in C++?

Ankith Reddy
Updated on 26-Jun-2020 13:34:14

4K+ Views

An object is an instance of a class. Memory is only allocated when an object is created and not when a class is defined.An object can be returned by a function using the return keyword. A program that demonstrates this is given as follows −Example Live Demo#include using namespace std; class Point {    private:    int x;    int y;    public:    Point(int x1 = 0, int y1 = 0) {       x = x1;       y = y1;    }    Point addPoint(Point p) {       Point temp;       temp.x = x + p.x;       temp.y = y + p.y;       return temp;    }    void display() {       cout

Advertisements