7A)Write an R program to demonstrate oop concepts, the construction and use of S3 and S4 classes.
S3 Classes: S3 classes are a simple and informal way of defining classes in R. They are often used for basic object-oriented programming. The key features of S3 classes include:
Informal Structure: S3 classes are more informal and flexible compared to S4 classes. They don't have a formal class definition.
Method Dispatch: Method dispatch in S3 is based on the class attribute of an object. Functions look at the class attribute to determine which method to use.
Not Strictly Typed: S3 classes are not strictly typed. Functions designed for a particular class often work as long as the object has the required structure.
S4 Classes: S4 classes are a more formal and structured way of defining classes in R. They provide a formal definition and are designed to support stricter object-oriented programming principles. The key features of S4 classes include:
Formal Structure: S4 classes have a formal and structured definition, providing more control over the class structure and behavior.
Method Dispatch: Method dispatch in S4 is more formalized and can be explicitly specified, offering greater control over method selection.
Strictly Typed: S4 classes are more strictly typed, meaning that the structure of the objects is explicitly defined.
CODE:
#List creation with its attributes name and roll no a=list(name="sahil",Roll_no=38) b=list(name="sahil",Roll_no=47) #defining a class "Student" class(a)="Student" class(b)="Student" #creation of object a b library(sloop) otype(a) otype(b) #create s4 object # setClass() command is used to create s4 class # the new() function is used to create an object in s4 class. # In this function we will pass the class name as # well as the value for slots # function setclass() command used to create # s4 class containing list oslotsFromS3() setClass("Student",slots =list(name="character",Roll_no="numeric")) # new keyword used to create object of class 'Student' c=new("Student",name="omji",Roll_no=48) d=new("Student",name="altaf",Roll_no=63) #calling object c otype(c)
OUTPUT :
$name [1] "sahil" $Roll_no [1] 38 attr(,"class") [1] "Student" $name [1] "sahil" $Roll_no [1] 47 attr(,"class") [1] "Student" [1] "S3" [1] "S3" An object of class "Student" Slot "name": [1] "omji" Slot "Roll_no": [1] 48 [1] "S4"
7B) Write an R program to define reference class and operation on them.
CODE:
#Reference class is an improvement over s4 class # here the methods belogs to the classes # these are the much similar to object-orinted classes # of the othelanguage # defining a Reference class is similar to defining s4 classes # we use setRefClass() instead of setClass() and # "Field" instead of "slots" library(methods) #setRefClass returns a generator movies =setRefClass("movies",fields = list(name="character",leadActor="character", rating="numeric")) # now we can use the generator to create objects movielist=movies(name="Iron man",leadActor="Robert Downey Jr",rating=7) movielist
OUTPUT :
Reference class object of class "movies" Field "name": [1] "Iron man" Field "leadActor": [1] "Robert Downey Jr" Field "rating": [1] 7