C++ Revision Day-2

ยท

5 min read

1) Difference b/w Java and C++

A) Goto Statements:-

  • C++: Supports goto statements, which allow the program to jump to another point in the code.
#include <iostream>
using namespace std;

int main() {
    int num = 1;
    label:
    cout << num << " ";
    num++;
    if (num <= 5) {
        goto label;  // Jumps back to the label
    }
    return 0;
}
  • Java: Does not support goto statements for better readability and maintainability.

B) System vs. Application Programming:-

  • C++: Commonly used for system-level programming like operating systems, game development, and embedded systems.

  • Java: is primarily used for application-level programming such as web, enterprise, and mobile applications.

C) Multiple Inheritance:-

  • C++ supports multiple inheritance wherein a class can inherit from more than one base class.
class A {
public:
    void display() { cout << "Class A" << endl; }
};

class B {
public:
    void display() { cout << "Class B" << endl; }
};

class C : public A, public B {
};

int main() {
    C obj;
    obj.A::display(); // Needs to specify which display() to call
    obj.B::display();
    return 0;
}
  • Java does not support multiple inheritance (to reduce complexity) instead it supports multiple interfaces.
interface A {
    void display();
}

interface B {
    void display();
}

class C implements A, B {
    public void display() {
        System.out.println("Class C");
    }
}

public class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.display();
    }
}

D) Operator Overloading:-

  • C++ does support operator overloading while Java does not.
class Complex {
public:
    int real, imag;
    Complex operator + (const Complex& obj) {
        Complex temp;
        temp.real = real + obj.real;
        temp.imag = imag + obj.imag;
        return temp;
    }
};

E) Pointers:-

  • C++ allows use of pointers, while Java uses pointers internally but does not allow it to be manipulated by the programmer for safety and security concerns.
int main() {
    int a = 10;
    int *p = &a;
    cout << "Value of a: " << *p << endl;
    return 0;
}

F) Compilation:-

  • C++: Uses a compiler that converts source code into machine code.

  • Java: Uses both a compiler (converts source code to bytecode) and an interpreter (JVM interprets bytecode at runtime).

G) Call by Value and Call by Reference:-

  • C++ supports both call-by value and references.

  • Java only supports call-by-value.

void swap(int &a, int &b) {  // Call by reference
    int temp = a;
    a = b;
    b = temp;
}
void swap(Integer a, Integer b) {  // Won't swap the actual values
    Integer temp = a;
    a = b;
    b = temp;
}

H) Structs and Unions:-

  • C++ supports structs and unions, while in Java everything is done using classes.
struct Person {
    string name;
    int age;
};

2) What is :: in C++?

:: is called a scope resolution operator which is used to access global variables with the same name as of local variables, for defining functions outside the class, for accessing static variables, and for referring to a class inside of another class.

3) What is endl in C++ ?

The endl manipulator can be used with the << operator to insert a newline character and flush the output stream.

4) How to include all libraries in C++ ?

#include <bits/stdc++.h>

5) What are the different data types present in C++?

The 4 data types in C++ are:

  • Primitive Datatype such as char, short, int, float, long, double, bool, etc.

  • Derived datatype such as an array, pointer, etc.

  • Enumeration such as enum

  • User-defined data types such as structure, class, etc.

6) Namespaces in C++ ?

In C++, a namespace is a declarative region that provides a scope to the identifiers (names of types, functions, variables, etc.) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

Definition and Usage

A namespace is defined using the namespace keyword followed by the name of the namespace. The content enclosed within the curly braces {} is part of the namespace.

#include <iostream>

namespace MyNamespace {
    int myVar = 10;

    void myFunction() {
        std::cout << "Hello from MyNamespace!" << std::endl;
    }
}

int main() {
    // Accessing a variable from MyNamespace
    std::cout << MyNamespace::myVar << std::endl;

    // Calling a function from MyNamespace
    MyNamespace::myFunction();

    return 0;
}

Using Declaration:-

  • This allows you to use specific members of a namespace without prefixing them with the namespace name.
using MyNamespace::myVar;
using MyNamespace::myFunction;

int main() {
    std::cout << myVar << std::endl;  // No need to prefix with MyNamespace::
    myFunction();  // No need to prefix with MyNamespace::
    return 0;
}

Using Directive:-

  • This allows you to use all members of a namespace without prefixing them with the namespace name.
using namespace MyNamespace;

int main() {
    std::cout << myVar << std::endl;  // No need to prefix with MyNamespace::
    myFunction();  // No need to prefix with MyNamespace::
    return 0;
}

Summary

Namespaces in C++ provide a way to group related code and prevent name collisions by providing scope to identifiers. They are a fundamental part of organizing large codebases and ensuring that different parts of a program or different libraries can coexist without interfering with each other.

ย