Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Oop 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Instructor:

Bipasha Majumder
Lecturer, Dept of CSE
Dhaka International University

1
C structures revisited
 Structure is unique feature in C language. Structures
provide a method for packing together data of different
types.
// structure example int main ()
struct student {
{ struct student a;
char name[20]; a.name=“Rhahim";
int roll; a.roll=111;
}; }

 Here a structure type student is created and a structure


variable “a” is declared of type struct student and assign
values to its different properties.
2
Limitations of C structures
 The standard C does not allow the struct data type to be
treated like built-in types.
 For the above structure student if we declare three variable
a, b, and c and assign values to a and b. Then if we write a
statement like c = a + b, will produce an error. Because C
does not permit addition or subtraction of complex
variables.
 Another important limitation of C structures is that they
do not permit data hiding. Structure members can be
directly accessed by the structure variables by any function
anywhere in their scope. In other words structure
members are public members.
3
Defining Class and Declaring Objects
 A Class is a user-defined data type that has data members and member
functions.
 An Object is basically class type variable or instance of a Class.
 A class is defined in C++ using the keyword class followed by the name of
the class. The body of the class is defined inside the curly brackets and
terminated by a semicolon at the end.
Defining Class and Declaring Objects
 When a class is defined, only the specification for the
object is defined; no memory or storage is allocated. To
use the data and access functions defined in the class,
you need to create objects.
Syntax

ClassName ObjectName;
Data Hiding with Access Specifier
Data hiding is the key feature of OOP.
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 later.
If the labels are missing, then, by default, all the
members are private.

6
Program to demonstrate accessing of data members
#include <bits/stdc++.h>
using namespace std;
class day_73 {
// Access specifier
public:
// Data Members
string name;
int id;
// Member Functions()
void print()
{ cout << "Name is:" <<name<<" "<<"Roll is:"<<id<<endl; }
};
int main()
{
// Declare an object of class day_73
day_73 obj1; Output:
// accessing data member
obj1.name = "Anis"; Name is:Anis Roll is:12
obj1.id=12;
// accessing member function
obj1.print();
return 0;
} 7
Create multiple objects of a class
Same as before.....
int main()
{
// Declare multiple object of class day_73
day_73 obj1,obj2;
// accessing data member
obj1.name = "Anis";
obj1.id=12;
obj2.name = "Jahid";
obj2.id=13;
Output:
// accessing member function Name is:Anis Roll is:12
obj1.print(); Name is:Jahid Roll is:13
obj2.print();
return 0;
} 8
Can we apply class_object concept without member
function?
Ans: Yes, you can! Need to print it manually without calling member function. But
good practice is using member function.
#include <bits/stdc++.h>
using namespace std;
class day_73 {
public:
string name;
int id;
}; Output:
int main()
{
Name:Anis Roll:12
day_73 obj1;
obj1.name = "Anis";
obj1.id=12;
cout << "Name:" <<obj1.name<<" "<<"Roll:"<<obj1.id<<endl;

return 0;
} 9
Take input from user and Print it!!
#include <bits/stdc++.h>
using namespace std;
class day_73 {
public:
string name;
int id;
};
int main()
{
day_73 obj1;
Cin>>obj1.name>>obj1.id;
cout << "Name:" <<obj1.name<<" "<<"Roll:"<<obj1.id<<endl;

return 0;
}

10
Practice!!
 Create a class named 'Student' with a string variable
'name‘, an integer variable 'roll_no‘, and floating
variable ‘cgpa’. Now, creating at least two object of the
class Student and print it.

11
Quiz 1!
What is the difference between struct and class in C++?

1. All members of a structure are public and structures don't have


constructors and destructors
2. Members of a class are private by default and members of struct are
public by default. When deriving a struct from a class/struct, default
access-specifier for a base class/struct is public and when deriving a
class, default access specifier is private.
3. All members of a structure are public and structures don't have virtual
functions
4. All of the above

12
Quiz 2!
Predict the output?
class Test {
int x;
};
1. 0
int main()
{ 2. Garbage Value
Test t; 3. Compiler Error
cout << t.x;
return 0;
}

13

You might also like