You are on page 1of 30

CLASS AND

STRUCTURE
Instructor
Dr Farzana Rahman
EXAMPLE
EXAMPLE
C++ OOP
 Class is a template for objects, and an object is an instance of a class.
 When the individual objects are created, they inherit all the variables and functions from the
class.
C++ OOP
 In real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
 Attributes and methods are basically variables and functions that
belongs to the class. These are often referred to as "class members".
 A class is a user-defined data type that we can use in our program,
and it works as an object constructor, or a "blueprint" for creating
objects.
CREATE A CLASS CALLED
"MYCLASS":
class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};

 The class keyword is used to create a class called MyClass.


 The public keyword is an access specifier, which specifies that members (attributes and methods)
of the class are accessible from outside the class. You will learn more about access specifiers later.
 Inside the class, there is an integer variable myNum and a string variable myString. When variables
are declared within a class, they are called attributes.
 At last, end the class definition with a semicolon ;
CREATE class MyClass {       // The class
  public:             // Access specifier

OBJECT     int myNum;        // Attribute (int variable)


    string myString;  // Attribute (string variable)
};
 To create an object
of MyClass, specify the class int main() {
name, followed by the object   MyClass myObj;  // Create an object of MyClass
name.
  // Access attributes and set values
  myObj.myNum = 15; 
 To access the class attributes   myObj.myString = "Some text";
(myNum and myString), use
the dot syntax (.) on the   // Print attribute values
object:   cout << myObj.myNum << "\n"; 
  cout << myObj.myString; 
  return 0;
}
CREATE int main() {

MULTIP   // Create an object of Car


  Car carObj1;
  carObj1.brand = "BMW";
LE   carObj1.model = "X5";
  carObj1.year = 1999;

OBJECT   // Create another object of Car


  Car carObj2;
  carObj2.brand = "Ford";
// Create a Car class with some   carObj2.model = "Mustang";
attributes   carObj2.year = 1969;
class Car {
  public:   // Print attribute values
    string brand;      cout << carObj1.brand << " " << carObj1.model << " " <<
    string model; carObj1.year << "\n";
    int year;   cout << carObj2.brand << " " << carObj2.model << " " <<
}; carObj2.year << "\n";
  return 0;
}
CLASS METHODS
 Methods are functions that belongs to the class.
 There are two ways to define functions that belongs to a class:
 Inside class definition
 Outside class definition
 ou access methods just like you access attributes; by creating an
object of the class and by using the dot syntax (.)
CLASS METHODS (INSIDE
EXAMPLE)
class MyClass {        // The class
  public:              // Access specifier
    void myMethod() {  // Method/function defined inside the class
      cout << "Hello World!";
    }
};

int main() {
  MyClass myObj;     // Create an object of MyClass
  myObj.myMethod();  // Call the method
  return 0;
}
CLASS METHOD (OUTSIDE
CLASS)
To define a function outside the class MyClass {        // The class
class definition,   public:              // Access specifier
    void myMethod();   // Method/function declaration
1. declare it inside the class };
2. define it outside of the
class by specifying the // Method/function definition outside the class
name of the class, followed void MyClass::myMethod() {
the scope   cout << "Hello World!";
resolution :: operator, }
followed by the name of
the function int main() {
  MyClass myObj;     // Create an object of MyClass
  myObj.myMethod();  // Call the method
  return 0;
}
CONSTRUCTOR
 A constructor in C++ is a special method that is automatically called when an object of a class is
created.
 To create a constructor, use the same name as the class, followed by parentheses ()

class MyClass {     // The class


  public:           // Access specifier
    MyClass() {     // Constructor
      cout << "Hello World!";
    }
};

int main() {
  MyClass myObj;    // Create an object of MyClass (this will call the constructor)
  return 0;
}
ACCESSS SPECIFIERS
In C++, there are three access specifiers:
 public - members are accessible from outside the class
 private - members cannot be accessed (or viewed) from
outside the class
 protected - members cannot be accessed from outside the
class, however, they can be accessed in inherited classes. You
will learn more about Inheritance late
ACCESSS SPECIFIERS
class MyClass {
  public:    // Public access specifier
    int x;   // Public attribute
  private:   // Private access specifier
    int y;   // Private attribute
};

int main() {
  MyClass myObj;
  myObj.x = 25;  // Allowed (public)
  myObj.y = 50;  // Not allowed (private)
  return 0;
}. Class_acess_sp.cpp
ACCESSS SPECIFIERS
 By default, all members of a class are private if you don't specify an access
specifier:

class MyClass {
  int x;   // Private attribute
  int y;   // Private attribute
};
It is possible to access private members of a class using a public method inside
the same class.
ENCAPSULATION
 The meaning of Encapsulation, is to make sure that "sensitive" data is hidden
from users.
 To achieve this, one must declare class variables/attributes as private (cannot
be accessed from outside the class).
 To read or modify the value of a private member, one can provide
public get and set methods.
ENCAPSULATION
class Employee { int main() {
  private:   Employee myObj;
    // Private attribute
    int salary;   myObj.setSalary(50000);
  cout << myObj.getSalary();
  public:   return 0;
    // Setter }
    void setSalary(int s) {
      salary = s;
    }
    // Getter
Encap.cpp
    int getSalary() {
      return salary;
    }
};
ENCAPSULATION
 The salary attribute is private, which have restricted access.
 The public setSalary() method takes a parameter (s) and assigns it to
the salaryattribute (salary = s).
 The public getSalary() method returns the value of the private salary attribute.
 Inside main(), we create an object of the Employee class. Now we can use
the setSalary() method to set the value of the private attribute to 50000. Then
we call the getSalary() method on the object to return the value.
INHERITANCE
 In C++, it is possible to inherit attributes and methods from one
class to another. We group the "inheritance concept" into two
categories:
 derived class (child) - the class that inherits from another class
 base class (parent) - the class being inherited from
 To inherit from a class, use the : symbol.
INHERITANCE
// Base class // Derived class
class Vehicle { class Car: public Vehicle {
  public:    public: 
    string brand = "Ford";     string model = "Mustang";
    void honk() { };
      cout << "Tuut, tuut! \n" ;
    } int main() {
};   Car myCar;
  myCar.honk();
  cout << myCar.brand + " " +
Inherit.cpp myCar.model;
  return 0;
}
MULTIPLE INHERITANCE
/ Another base class
 A class can also be derived from more than one class MyOtherClass {
base class, using a comma-separated list:   public: 
    void myOtherFunction() {
      cout << "Some content in another class." ;
    }
// Base class };
class MyClass { // Derived class 
  public: 
    void myFunction() { class MyChildClass: public MyClass, public MyOtherClass 
{
      cout << "Some content in parent };
class." ;
    } int main() {
  MyChildClass myObj;
};   myObj.myFunction();
  myObj.myOtherFunction();
multi_inherit.cpp   return 0;
}
ACCESS SPECIFIER
(PROTECTED)
 The specifier, protected, is similar to private, but it can also be
accessed in the inherited class

 Example: protect.cpp
C++ STRUCTURE
 A structure is a user-defined data type in C/C++.

 A structure creates a data type that can be used to group items of possibly
different types into a single type.
C++ STRUCTURE
.
STRUCTURE SYNTAX
 The ‘struct’ keyword is used to create a structure. The general
syntax to create a structure is as shown below:
struct structureName {
member1;
member2;
member3; . . .
memberN; };
Struct newstruct {

STRUCTURE // Data Members


int roll;

SYNTAX int age;


int marks;
 Structures in C++ can contain two types of
members:       

 Data Member: These members // Member Functions


areStructure syntax normal C++ variables. void printDetails()
We can create a structure with variables of
{
different data types in C++.
    cout<<"Roll = "<<roll<<"\n";
 Member Functions: These members are
normal C++ functions. Along with     cout<<"Age = "<<age<<"\n";
variables, we can also include functions     cout<<"Marks = "<<marks;
inside a structure declaration.
}
};
DECLARE STRUCTURE
VARIABLES
 A structure variable can either be declared with // A variable declaration like basic data
structure declaration or as a separate declaration types
like basic types
struct Point
// A variable declaration with structure
{
declaration.
   int x, y;
struct Point
}; 
{
int main()
   int x, y; {
} p1;     struct Point p1; 
// The variable p1 is declared with 'Point' // The variable p1 is declared like
   a normal variable
DECLARE STRUCTURE
VARIABLES
 Structure members cannot be initialized with declaration

struct Point
{
   int x = 0;  // COMPILER ERROR:  cannot initialize members here
   int y = 0;  // COMPILER ERROR:  cannot initialize members here
}; 
ACCESS STRUCTURE
ELEMENTS
 Structure members are accessed using dot (.) operator

struct_access.cpp
 Like other primitive data types, we can create an array of structures.

Struct_arr.cpp
 Like primitive types, we can have pointer to a structure. If we have a pointer to structure,
members are accessed using arrow ( -> ) operator instead of the dot (.) operator.
Struct_point.cpp
STRUCTURE VS CLASS

LETS DISCUSS

You might also like