What is Classes?

  • A class is an abstract data type containing data, a set of functions to access and manipulate the data, and a set of access restrictions on the data and on the functions.
  • You can think of a class as a template or a blueprint for an object. This blueprint defines attributes for storing data and defines operations for manipulating that data. A class also defines a set of restrictions to allow or deny access to its attributes and operations
  • Classes are templates for objects
  • They describe the kind and amount of data that an object will contain, but they do not represent any particular instance of an object
  • An Example of class might be “Car”-the abstract idea of what a car is.
  • Car has an engine, four wheels, a body color, an individual fuel efficiency and many other properties
  • The actions that are performed by the car could be; roll forward, turn on windshield wipers and so on
  • Thus the Car class has the properties and actions (behaviors). The class would not represent any particular car. Your car on the other hand is an object. It has a specific color, a specific engine etc. A different car might have different values for each of these properties, but both would be recognizable as being an instance of the Car class
Creating Classes And Structure
  • A new class can be created with the keyword Class
Public Class Add
class member implementation
End Class
  • A new structure can be created with the keyword Structure
Public Structure Add
structure member implementation
End Class
Adding Member
  • A class comprises of everything between the Class keyword and the End Class keyword
Public Class Add
Public a As Integer
End Class
Nested Types
  • Types can contain other types. Types within types are called nested types. Using classes as an example, a nested class usually represents an object that the parent class might need to create and manipulate, but which an external object never need to create independently
Program
Public Class Form1
Public Class Test
Public Structure ValueTest
Public t1 As Integer
End Structure
Public Class RefTest
Public t2 As Integer
End Class
Public Sub CreateTypes()
Dim TestStructure As ValueTest
TestStructure = New ValueTest()
TestStructure.t1 = 21
MessageBox.Show(TestStructure.t1, "Testing structure")
Dim TestClass As RefTest
TestClass = New RefTest()
TestClass.t2 = 51
MessageBox.Show(TestClass.t2, "Testing Class")
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim ob1 As New Test()
ob1.CreateTypes()
End Sub
End Class


Classes Vs Structure
  • The key difference between Classes and Structures is that Classes are reference types and Structures are value types
  • On a low level, this means that the instance data for classes is allocated on the heap, where as instance data for structures is allocated on the stack
  • Access to the stack is designed to be light and fast, but storage of large amount of data on the stack can impede overall application performance
  • In practical terms, that structures are best used for smaller, lightweight objects that contain relatively little instance data or for objects that do not persist for long
  • Classes are best suited for larger objects that contain more instance data and are expected to exist in memory for extended period

Reference Types and Value Types << Previous
Next >> Constructors and Destructors

Our aim is to provide information to the knowledge


comments powered by Disqus






Footer1