In Visual Basic, how are classes created differently from Java and C++? Create aBook class with properties using VB syntax.IGNOU

KINDLY CONTACT US ON OUR WHATSAPP NUMBER 9009368238

QUESTION:In Visual Basic, how are classes created differently from Java and C++? Create a
Book class with properties using VB syntax.

ANSWER:Great question! While Java, C++, and Visual Basic are all object-oriented programming languages, they do have some syntax differences when it comes to defining classes.

In Java:

public class Book {
    private String title;
    private String author;
    private int pages;

    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public int getPages() {
        return pages;
    }
}

In C++:

class Book {
private:
    std::string title;
    std::string author;
    int pages;

public:
    Book(std::string t, std::string a, int p) : title(t), author(a), pages(p) {}

    std::string getTitle() {
        return title;
    }

    std::string getAuthor() {
        return author;
    }

    int getPages() {
        return pages;
    }
};

In Visual Basic, the syntax is a bit more verbose and uses properties differently:

Public Class Book
    Private _title As String
    Private _author As String
    Private _pages As Integer

    Public Sub New(ByVal title As String, ByVal author As String, ByVal pages As Integer)
        _title = title
        _author = author
        _pages = pages
    End Sub

    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Public Property Author() As String
        Get
            Return _author
        End Get
        Set(ByVal value As String)
            _author = value
        End Set
    End Property

    Public Property Pages() As Integer
        Get
            Return _pages
        End Get
        Set(ByVal value As Integer)
            _pages = value
        End Set
    End Property
End Class

Leave a Reply

Your email address will not be published. Required fields are marked *